firebase云功能http请求无法正常工作?

发布于 2025-01-24 02:40:38 字数 1587 浏览 0 评论 0原文

当我在Postman上击中API时,我的云功能API无法正常工作,没有错误,只需在Postman上获得404即可。附加了代码和屏幕截图。

index.js

const functions = require("firebase-functions");
const admin = require("firebase-admin");

var serviceAccount = require("./key.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

const express = require("express");
const cors = require("cors");

// main app
const app = express();
app.use(cors({ origin: true }))

// main database refference
const db = admin.firestore();

// Routes test
app.get("/", (req, res) => {
    return res.status(200).send("test api ")
})

// create
app.post("api/create", async (req, res) => {
    console.log(req)
    try {
        console.log(req)
        await db.collection(useDetails).doc(`/${Date.now()}/`).create({
            id: Date.now(),
            name: req.body.name,
            mobile: req.body.mobile,
            add: req.body.add
        })
        return res.status(200).send({ status: "Sucess", msg: "Data Saved" });
    } catch (error) {
        console.log(error)
        return res.status(200).send({ status: "Failed", msg: "Data not Saved" });
    }
})

exports.app = functions.https.onRequest(app)

firebase仿真器也工作正常

当我在邮递员上击中API时,这是我得到的回应

My cloud functions API is not working properly when I hit api on postman, there are no error, just getting 404 on postman. Code and screenshots are attached.

index.js

const functions = require("firebase-functions");
const admin = require("firebase-admin");

var serviceAccount = require("./key.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

const express = require("express");
const cors = require("cors");

// main app
const app = express();
app.use(cors({ origin: true }))

// main database refference
const db = admin.firestore();

// Routes test
app.get("/", (req, res) => {
    return res.status(200).send("test api ")
})

// create
app.post("api/create", async (req, res) => {
    console.log(req)
    try {
        console.log(req)
        await db.collection(useDetails).doc(`/${Date.now()}/`).create({
            id: Date.now(),
            name: req.body.name,
            mobile: req.body.mobile,
            add: req.body.add
        })
        return res.status(200).send({ status: "Sucess", msg: "Data Saved" });
    } catch (error) {
        console.log(error)
        return res.status(200).send({ status: "Failed", msg: "Data not Saved" });
    }
})

exports.app = functions.https.onRequest(app)

Firebase Emulator is also working fine
enter image description here

when I hit api on postman here is the response i get
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2025-01-31 02:40:38
app.post("/api/create", async (req, res) => {

使用 /启动API地址

app.post("/api/create", async (req, res) => {

start api address with /

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文