1 回答
TA贡献1963条经验 获得超6个赞
如 Express文档中所述:
res.json([body])发送 JSON 响应。此方法发送一个响应(具有正确的内容类型),该响应是使用 .json 转换为 JSON 字符串的参数
JSON.stringify()。参数可以是任何 JSON 类型,包括对象、数组、字符串、布尔值、数字或 null,您也可以使用它来将其他值转换为 JSON。
在我们通过评论/聊天进行“调试”之后,似乎
{message: response}您传递给json()的对象会生成错误。
遵循HTTP Cloud Functions 文档,其中指出:
重要提示:确保所有 HTTP 函数都正确终止。通过正确终止函数,您可以避免因运行时间过长的函数而产生过多费用。
res.redirect()使用、res.send()或终止 HTTP 函数res.end()。
并且由于您在聊天中解释说您“只需要返回状态代码”并且您“想要将 json 数据保存到:admin.database().ref(/venue-menus/${locationId}/menu)”,
我建议你这样做:
exports.doshiiMenuUpdatedWebhook = functions.https.onRequest((req, res) => {
if (req.method === 'PUT') {
return res.status(403).send('Forbidden!');
}
cors(req, res, () => {
let verify = req.query.verify;
if (!verify) {
verify = req.body.verify;
}
let locationId = req.body.data.locationId
let posId = req.body.data.posId
let type = req.body.data.type
let uri = req.body.data.uri
let itemUri = req.body.data.itemUri
const options = {
headers: { 'authorization': 'Bearer ' + req.query.verify }
};
axios.get(uri, options)
.then(response => {
console.log('response data: ', response.data);
return admin.database().ref(`/venue-menus/${locationId}/menu`).set(response.data)
})
.then(response => {
return res.status(200).end()
})
.catch(err => {
return res.status(500).send({
error: err
})
})
})
});
添加回答
举报
