Abdur Rehman Khalid
Abdur Rehman Khalid

Reputation: 85

firebase cloud function http request is not working properly?

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

Upvotes: 0

Views: 1047

Answers (1)

Pratik Ranpariya
Pratik Ranpariya

Reputation: 121

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

start api address with /

Upvotes: 1

Related Questions