Javascript &引用;将循环结构转换为JSON“;尽管函数正确执行,但仍出现错误

Javascript &引用;将循环结构转换为JSON“;尽管函数正确执行,但仍出现错误,javascript,json,http,promise,axios,Javascript,Json,Http,Promise,Axios,我得到以下错误: TypeError:将循环结构转换为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_mo

我得到以下错误:

TypeError:将循环结构转换为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)

记录所有参数表明它们不是问题所在。ZohoAPI的配置也不是问题,因为这些条目被正确地输入到CRM中。我的假设是,它必须与JSON格式的HTTP响应有关。

您试图将
承诺转换为
JSON
,但不起作用。您的
createLead
函数返回一个
promise
,而不是
JSON
promise
是“循环对象”。

即使我使用了
await
,它仍然会返回承诺吗?将
async
添加到
函数中会导致
函数返回
promise
await
不会更改我们正在等待的函数的返回类型。
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)
}