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上的数据库。创建一个新的通知集合。谁能帮我吗?