Home > front end >  Unable to fetch the firebase data in the node js api
Unable to fetch the firebase data in the node js api

Time:08-08

I am trying to fetch some data from firebase datastore. When tring to push the same data to a node js based rest api, its not showing any data but no error as well. And while checking though postman, its showing 200 OK.

  • blog.js [Showing data as expected]
import { db } from "../configAuth/firebase-admin-config.js";

async function getBlogs() {
  const querySnapshot = await db.collection("myBlogs").get();
  querySnapshot.forEach((doc) => {
    console.log(doc.id, " => ", doc.data())
  });
}

export default getBlogs = getBlogs();

But when importing the getBlogs to the index.js, its not showing any data via http://localhost:5000/api/blogs which is my end point.

  • index.js
import express from "express";
import getBlogs from "./blogs/blog.js";

const app = express();
app.use(express.json());

app.get("/", (req, res) => {
  res.send("API is running...");                //working
});

app.get("/api/blogs", (req, res) => {
  getBlogs.then((blogs) => {                  // not working
    res.json(blogs);
  });
});

const PORT = 5000;
app.listen(PORT, () => console.log(`Server sarted on PORT ${PORT}`));

If I am trying to loading data from a data.json file, index.js is able to load that data correcty and I am able to see the data in http://localhost:5000/api/blogs as well. And able to push the data to the frontend which us using react native.

Getting the following out put which is coming from blog.js:

[nodemon] starting `node src/index.js`
Server sarted on PORT 5000
YzBuR9QG4xaVzcXU3JyP  =>  {
  blogId: '9087612312390871231',
  createdAt: Timestamp { _seconds: 1659399096, _nanoseconds: 632000000 },
  content: 'content of testing firebase 01',
  createdBy: 'Udayendu Kar',
  published: true,
  updatedAt: 0,
  tags: [ 'firebase', 'development' ],
  title: 'testing firebase'
}
yUsB7V79Vhs9uCQPlhg0  =>  {
  blogId: '9087612312390871232',
  published: 'false',
  createdBy: 'Udayendu Kar',
  tags: [ 'nodejs', 'react' ],
  updatedAt: '0',
  createdAt: Timestamp { _seconds: 1659780527, _nanoseconds: 628000000 },
  title: 'testing node 02',
  content: 'content of testing node 02'
}

CodePudding user response:

Since getBlogs is an async function, your export is a Promise and not the value you want to send to the caller.

To send the actual value to the response, you can use await or then:

app.get("/api/blogs", (req, res) => {
  getBlogs.then((blogs) => {
    res.json(blogs);
  })
});

Also: your getBlogs function is not returning any value. Did you mean for it to be:

async function getBlogs() {
  const querySnapshot = await db.collection("myBlogs").get();
  return querySnapshot.doc.map((doc) => doc.data());
}

CodePudding user response:

Update your route as follows

app.get("/api/blogs", getBlogs);

and return the result from the calling function.

async function getBlogs() {
  const querySnapshot = await db.collection("myBlogs").get();
  querySnapshot.forEach((doc) => {
    console.log(doc.id, " => ", doc.data())
  });
  return querySnapshot;
}
  • Related