Firebase云端函数 – 序列化返回值时出错:

我有一个云function用来交叉引用两个列表,并find在列表中相互匹配的值。 该函数似乎正常工作,但在日志中,我不断看到这个Error serializing return value: TypeError: Converting circular structure to JSON 。 这是function…

 exports.crossReferenceContacts = functions.database.ref('/cross-ref-contacts/{userId}').onWrite(event => { if (event.data.previous.exists()) { return null; } const userContacts = event.data.val(); const completionRef = event.data.adminRef.root.child('completed-cross-ref').child(userId); const removalRef = event.data.ref; var contactsVerifiedOnDatabase ={}; var matchedContacts= {}; var verifiedNumsRef = event.data.adminRef.root.child('verified-phone-numbers'); return verifiedNumsRef.once('value', function(snapshot) { contactsVerifiedOnDatabase = snapshot.val(); for (key in userContacts) { //checks if a value for this key exists in `contactsVerifiedOnDatabase` //if key dioes exist then add the key:value pair to matchedContacts }; removalRef.set(null); //remove the data at the node that triggered this onWrite function completionRef.set(matchedContacts); //write the new data to the completion-node }); }); 

我试着把completionRef.set(matchedContacts);放在前面completionRef.set(matchedContacts); 但是这仍然给我错误。 不知道我做错了什么,如何摆脱错误。 谢谢你的帮助

在返回Firebase数据库上的多个交易承诺时,我遇到了完全相同的问题。 起初我打电话给:

 return Promise.all(promises); 

我的promises对象是一个数组,我正在使用的地方是通过调用promises.push(<add job here>)来推送所有需要执行的promises.push(<add job here>) 。 我想这是执行这些工作的有效方式,因为现在这些工作将会同时运行。

云function工作,但我得到了你描述的完全相同的错误。

但是,正如迈克尔·布莱(Michael Bleigh)在他的评论中所build议的那样,加上then解决了这个问题,我不再看到这个错误

 return Promise.all(promises).then(() => { return; }).catch(er => { console.error('...', er); }); 

如果这不能解决你的问题,也许你需要将你的循环对象转换为JSON格式。 一个例子是写在这里,但我没有试过: https : //stackoverflow.com/a/42950571/658323 (它使用circular-json库)。

尝试:

 return verifiedNumsRef.once('value').then(function(snapshot) { contactsVerifiedOnDatabase = snapshot.val(); for (key in userContacts) { //checks if a value for this key exists in `contactsVerifiedOnDatabase` //if key dioes exist then add the key:value pair to matchedContacts }; return Promise.all([ removalRef.set(null), //remove the data at the node that triggered this onWrite function completionRef.set(matchedContacts) ]).then(_ => true); }); 

我有相同的错误输出与一个非常相似的设置,并不知道如何摆脱这个错误。 我不完全确定是否所有的精华都被以前的答案所捕获,所以我将离开你我的解决scheme,也许它可以帮助你。

最初我的代码看起来像这样:

 return emergencyNotificationInformation.once('value', (data) => { ... return; }); 

但添加后,然后赶上错误消失。

 return emergencyNotificationInformation.once('value') .then((data) => { ... return; }) .catch((error) => { ... return: }); }