最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onCall cloud function returns 'Data cannot be encoded in JSON. NAN' - Stack Overflow

programmeradmin6浏览0评论

I have an onCall function that looks like this..

exports.getResults = functions.https.onCall((data, context) => {
  const admin = context.auth.token.admin
  const uniqueId = context.auth.uid
  const doc = db.collection('collection').doc(data.docId)

    return doc
      .get()
      .then(querySnapshot => {
        return querySnapshot.docs.map(doc => {
          const doc = doc.data()
          const collectionDoc = {
            docValues
          }
    
          return collectionDoc
        })
      })
      

my console.log prints the doc I am requesting as an object, so I'm not sure what the issue is?

I have an onCall function that looks like this..

exports.getResults = functions.https.onCall((data, context) => {
  const admin = context.auth.token.admin
  const uniqueId = context.auth.uid
  const doc = db.collection('collection').doc(data.docId)

    return doc
      .get()
      .then(querySnapshot => {
        return querySnapshot.docs.map(doc => {
          const doc = doc.data()
          const collectionDoc = {
            docValues
          }
    
          return collectionDoc
        })
      })
      

my console.log prints the doc I am requesting as an object, so I'm not sure what the issue is?

Share Improve this question edited Mar 28, 2021 at 8:46 invrt asked Aug 31, 2020 at 3:36 invrtinvrt 7098 silver badges28 bronze badges 1
  • What exactly are you expecting this function to return other than an error? What is the log printing? Please edit the question to explain what is not working the way you expect. – Doug Stevenson Commented Aug 31, 2020 at 4:31
Add a ment  | 

1 Answer 1

Reset to default 7

I understand that you have this error in Firebase Function logs. This means that you are returning NaN somewhere in the object. According to this article:

To send data back to the client, return data that can be JSON encoded.

It may depend on implementation, however here NaN is cannot be JSON encoded. (Found something about it in Wikipedia - Data types and syntax - Number).

You can easily replicate this problem by deploying function with not initialized variable:

exports.getResults = functions.https.onCall(() => {
        var number;
        var value = number+1;
        return {
                value,
                "text": "test",
        };
})

In the example I added undefined to number the result will be NaN. Of course there might be other functions that will return NaN as well. Anyway if you deploy example above, it will log the same error "Data cannot be encoded in JSON. NaN". I think this is most easily to be overlooked in the code.

I think you have to carefully check all data that you are returning.

发布评论

评论列表(0)

  1. 暂无评论