Reputation: 17
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);
});
});
This is my function. When I deploy and try to post request on Postman like this:
{
"body": "New Notice",
"userHandle": "user"
}
It says Error: could not handle the request. I want it to update my database on Firebase. Creating a new notice collection. Can anyone Help me?
Upvotes: 0
Views: 1416
Reputation: 83191
You have two mistakes in your code, when you create the Timestamp
.
fromDate()
not fromData()
;admin.firestore.Timestamp.fromDate(new Date())
In addition note that you should do as follows for the first block in your code. No need to use return
, see the doc.
if (req.method !== "POST") {
res.status(400).json({ error: "Method not allowed" });
}
Upvotes: 1