Firebase 函数未向客户端应用返回响应

在 firebase 中运行可调用函数。在 Firebase 模拟器中本地运行它。我返回一个简单的字符串只是为了看看我是否可以将其取回给客户端,但我在响应中得到的只是。我不完全确定这里的问题是什么。我确信有些东西我不了解承诺,如果你在这个项目的另一部分通过另一个问题帮助我,我很感激。仍然试图理解所有这些是如何工作的。这是函数:{"data": null}


exports.createAssetMux = functions.https.onCall((data, context) => {


    console.log('data: ', data)


    admin.firestore().collection('users').doc(data.id).update({

        streamID: '98273498237'

    }).then(() => {

        console.log('update success')

        return {data: 'update successful'}

    }).catch(error => {

        console.log('error message: ', error)

        return {data: 'there was an issue updating document'}

    });

});

此时只是尝试将数据返回到客户端。我在Firebase功能上遇到了一些其他问题,我正在尝试解决,所有这些都是相互关联的,只是不想使另一篇文章过于复杂。任何帮助都会很棒!提前致谢!


慕运维8079593
浏览 62回答 1
1回答

哆啦的时光机

返回语句未执行预期的操作。它们只是从您传递给的 lambda 函数返回一个值, 和 .它们不会从传递给 的较大函数返回值。thencatchonCall可调用函数要求您返回一个承诺,该承诺使用要发送到客户端的对象进行解析。现在,传递给的函数始终不返回任何内容。您所要做的就是返回整个承诺链:onCall    return admin.firestore().collection('users').doc(data.id).update({        streamID: '98273498237'    }).then(() => {        console.log('update success')        return {data: 'update successful'}    }).catch(error => {        console.log('error message: ', error)        return {data: 'there was an issue updating document'}    });我添加的只是第一行开头的返回。了解JavaScript承诺如何工作对于编写有效的函数至关重要。我强烈建议花一些时间来学习他们通常的模式。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript