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
})
})
})
});
添加回答
舉報