首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在节点js中获取防火墙数据。

无法在节点js中获取防火墙数据。
EN

Stack Overflow用户
提问于 2022-08-07 11:21:54
回答 2查看 53关注 0票数 1

我正在尝试从火基数据库中获取一些数据。当tring将相同的数据推送到基于js的rest节点时,它不会显示任何数据,但也不会显示错误。当检查邮递员时,它显示200 OK。

  • blog.js [Showing data as expected]

代码语言:javascript
运行
复制
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();

但是当将getBlogs导入到index.js时,它没有通过http://localhost:5000/api/blogs显示任何数据,这是我的终点。

  • index.js

代码语言:javascript
运行
复制
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}`));

如果我试图从data.json文件中加载数据,那么index.js能够正确地加载该数据,我也能够在http://localhost:5000/api/blogs中看到数据。并能够将数据推送到我们使用的本机反应的前端。

blog.js获取以下输出put

代码语言:javascript
运行
复制
[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'
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-07 14:15:32

因为getBlogs是一个async函数,所以您的导出是一个Promise,而不是要发送给调用者的值。

要将实际值发送到响应,可以使用awaitthen

代码语言:javascript
运行
复制
app.get("/api/blogs", (req, res) => {
  getBlogs.then((blogs) => {
    res.json(blogs);
  })
});

另外:您的getBlogs函数没有返回任何值。你的意思是:

代码语言:javascript
运行
复制
async function getBlogs() {
  const querySnapshot = await db.collection("myBlogs").get();
  return querySnapshot.docs.map((doc) => doc.data());
}
票数 1
EN

Stack Overflow用户

发布于 2022-08-07 16:45:52

按以下方式更新您的路线

代码语言:javascript
运行
复制
app.get("/api/blogs", getBlogs);

并返回调用函数的结果。

代码语言:javascript
运行
复制
async function getBlogs() {
  const querySnapshot = await db.collection("myBlogs").get();
  querySnapshot.forEach((doc) => {
    console.log(doc.id, " => ", doc.data())
  });
  return querySnapshot;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73266968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档