将请求发布到Firebase函数时,邮递员说“错误:无法处理请求”

Akshaypx

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

雷诺·塔内克(Renaud Tarnec)

创建时,您的代码中有两个错误Timestamp

  1. 有一个错字:方法fromDate()不是fromData()
  2. 您需要按以下方式调用它: admin.firestore.Timestamp.fromDate(new Date())

另外请注意,您应该对代码中的第一个块执行以下操作。无需使用return,请参阅文档

  if (req.method !== "POST") {
    res.status(400).json({ error: "Method not allowed" });
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章