您好,我是新的 firebase 函数,并尝试将数据推送到 firestore,通过下面的代码,我可以将消息推送到 firestore 并且它正在工作,
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into Firestore using the Firebase Admin SDK.
const writeResult = await admin.firestore().collection('messages').add({original: original});
// Send back a message that we've successfully written the message
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
通过这个网址我可以推送文本
http://localhost:5001/myProj/us-central1/addMessage?text=sss
我需要将一些数据推送到对象数组中来代替文本,我需要对上面的代码进行哪些更改。
对象如:
data: { booking_id: "0",
channels: "4",
country: "GB",
date: "2020-05-12",
currencies: "GBP",
total: "5000" }
如果您想在 Cloud Function 中传递对象,则应将其添加到 POST 请求的正文中并按照中所述进行解析这部分文档 https://firebase.google.com/docs/functions/http-events#read_values_from_the_request并在此详细说明官方样本 https://github.com/firebase/functions-samples/tree/main/quickstarts/time-server(见下文代码行 https://github.com/firebase/functions-samples/blob/3515b7f38a3c598cdb20152a263372e81719ecda/quickstarts/time-server/functions/index.js#L69).
更具体地说,在云功能中您应该执行以下操作:
const booking_id = req.body.booking_id;
const channels = req.body.channels;
//...
并通过curl调用它,您应该执行以下操作:
curl -H 'Content-Type: application/json' /
-d '{"booking_id": "0", "channels": "4", "country": "GB", ...}' /
https://us-central1-<project-id>.cloudfunctions.net/....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)