0
votes

I'm getting the following error:

TypeError: Converting circular structure to JSON

 at JSON.stringify (<anonymous>)
 at stringify (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/response.js:1123:12)
 at ServerResponse.json (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/response.js:260:14)
 at cors (/Users/landing-page-backend/functions/zohoCrmHook.js:45:43)
 at process._tickCallback (internal/process/next_tick.js:68:7)

for the HTTP response in my createLead function, despite the fact that the function is executed properly and does what it's supposed to do (which is to create an entry in my CRM).

I've pointed out in the following code where the error occurs:

const axios = require('axios');
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true })

const clientId = functions.config().zoho.client_id;
const clientSecret = functions.config().zoho.client_secret;
const refreshToken = functions.config().zoho.refresh_token;
const baseURL = 'https://accounts.zoho.com';



module.exports = (req, res) => {
    cors(req, res, async () => {        
        const newLead = {
            'data': [
            {
                'Email': String(req.body.email),
                'Last_Name': String(req.body.lastName),
                'First_Name': String(req.body.firstName),
            }
            ],
            'trigger': [
                'approval',
                'workflow',
                'blueprint'
            ]
        };
        
        const { data } = await getAccessToken();
        const accessToken = data.access_token;

        const leads = await getLeads(accessToken);
        const result = checkLeads(leads.data.data, newLead.data[0].Email);
        
        if (result.length < 1) {
            try {
                return res.json(await createLead(accessToken, newLead)); // this is where the error occurs
            } catch (e) {
                console.log(e); 
            }
        } else {
            return res.json({ message: 'Lead already in CRM' })
        }
    })
}

function getAccessToken () {
    const url = `https://accounts.zoho.com/oauth/v2/token?refresh_token=${refreshToken}&client_id=${clientId}&client_secret=${clientSecret}&grant_type=refresh_token`;
  
    return new Promise((resolve, reject) => {
      axios.post(url)
        .then((response) => {
          return resolve(response);
        })
        .catch(e => console.log("getAccessToken error", e))
    });
}

function getLeads(token) {
    const url = 'https://www.zohoapis.com/crm/v2/Leads';
  
    return new Promise((resolve, reject) => {
      axios.get(url, {
        headers: {
          'Authorization': `Zoho-oauthtoken ${token}`
        }
      })
        .then((response) => {
          return resolve(response);
        })
        .catch(e => console.log("getLeads error", e))
    })
}
  
function createLead(token, lead) {
    const url = 'https://www.zohoapis.com/crm/v2/Leads';

    return new Promise((resolve, reject) => {
        const data = JSON.stringify(lead);
        axios.post(url, data, {
            headers: {
                'Authorization': `Zoho-oauthtoken ${token}`
            }
        })
        .then((response) => {
            console.log("response in createLead", response)
            return resolve(response);
        })
        .catch(e => reject(e))
    })
}

function checkLeads(leads, currentLead) {
    return leads.filter(lead => lead.Email === currentLead)
}

Console.logging all the parameters indicate that the they are not the issue. The configuration of the Zoho API is, also, not the issue considering the fact that the entries are being properly made into the CRM. My assumption is that it would have to do with the HTTP response in the JSON format.

1

1 Answers

0
votes

You're trying to convert a promise to JSON, not going to work. Your createLead function returns a promise, not a JSON. The promise is the 'circular object'.