我正在向包含以下正文的云函数发送发布请求:
{message: "this is the message"}
如果我尝试打印请求的整个正文,它会显示它,但如果我尝试获取 message
字段,我会在控制台中得到 undefined
。
这是我的功能:
exports.myCloudFunction = functions.https.onRequest((req: any, res: any) => {
console.log(req.body)\\prints the body of the request fine
console.log(req.body.message)\\prints "undefined"
cors(req, res, () => {
const pl = req.body.message;
console.log(pl);\\prints "undefined"
});
return res.send("all done")
});
最佳答案
如此处其他答案中所述,您不需要 Cloud Functions 的正文解析器。云功能将 automatically parse JSON并将解析后的 JSON 对象放入 body
属性中。如果您希望它自动发生,您应该将请求的内容类型设置为“application/json”,如链接文档中所述。然后您可以按预期使用 req.body
。
关于node.js - 从云函数内的请求主体访问各个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63547622/