akshaypx
akshaypx

Reputation: 17

Postman says "Error: could not handle the request" when posting request to Firebase function


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

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83191

You have two mistakes in your code, when you create the Timestamp.

  1. There is a typo: the method is fromDate() not fromData();
  2. You need to call it as follows: 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

Related Questions