【问题标题】:Postman says "Error: could not handle the request" when posting request to Firebase function邮递员在向 Firebase 函数发布请求时说“错误:无法处理请求”
【发布时间】: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


【解决方案1】:

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

  1. 有一个错字:方法是fromDate()不是fromData()
  2. 你需要这样称呼它:admin.firestore.Timestamp.fromDate(new Date())

另外请注意,您应该对代码中的第一个块执行以下操作。不用return,见doc

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2020-12-11
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 2018-11-07
    相关资源
    最近更新 更多