嗨,我是新的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.`});
});
使用这个url我可以推送文本
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请求的主体中,并按照文档的这一部分和这个官方示例(请参阅下面的代码行)中所解释的方式对其进行解析。
更具体地说,在Cloud Function中你应该这样做:
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/....