我是谷歌云功能和 Node Js 的新手,当我尝试解析 Json 负载时,不断收到此错误
exports.processdata = (event, context) => {
const pubsubMessage = event;
var obj = JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString());
console.log(obj.temp);
};
错误
TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
Function.Buffer.from
CLoud 函数在 Node.Js 版本 8 上运行
最佳答案
您可以使用属性.json读取json负载,下面是完整的代码
const functions = require('firebase-functions');
exports.processdata = functions.pubsub.topic('topic-np').onPublish((message) => {
// [START readJson]
// Get the `name` attribute of the PubSub message JSON body.
let name = null;
try {
name = message.json.name;
} catch (e) {
console.error('PubSub message was not JSON', e);
}
引用:https://firebase.google.com/docs/functions/pubsub-events
如果您正在测试在 Pub/Sub 上触发的云函数,请在主题中发布消息,然后通过云函数中的日志进行验证。
关于node.js - 如何解析从Pub/sub触发的Cloud Function中的Json负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59942704/