【发布时间】:2020-11-10 11:47:48
【问题描述】:
exports.createNotice = functions.https.onRequest((req, res) => {
if (req.method !== "POST") {
return res.status(400).json({ error: "Method not allowed" });
}
const newNotice = {
body: req.body.body,
userHandle: req.body.userHandle,
createdAt: admin.firestore().Timestamp.fromData(new Date()),
};
admin
.firestore()
.collection("notices")
.add(newNotice)
.then((doc) => {
res.json({ message: `document ${doc.id} created successfully` });
})
.catch((err) => {
res.status(500).json({ error: "something went wrong" });
console.error(err);
});
});
这是我的功能。当我像这样部署并尝试在 Postman 上发布请求时:
{
"body": "New Notice",
"userHandle": "user"
}
它显示错误:无法处理请求。我希望它更新我在 Firebase 上的数据库。创建一个新的通知集合。谁能帮帮我?
【问题讨论】:
-
Cloud Functions 控制台中是否出现任何错误?
标签: javascript node.js firebase google-cloud-firestore google-cloud-functions