Node js 、 mongo DB 使用异步从多个集合中获取数据

发布于 2025-01-10 15:53:10 字数 986 浏览 0 评论 0原文

您好,我正在尝试从数据库获取我所有的博客并更改作者 来自数据库中另一个集合的名称,然后将它们渲染到 ejs 文件,但是页面在数组填满之前呈现

app.get('/', async (req, res) => {
      let blogList = new Array(); 
      await Blog.find({}, function (err, foundBlog) { 
        console.log(foundBlog);
        if (err) {
          console.log(err);
        } else {
          foundBlog.forEach(async (blog) => {
            await Author.findById(blog.author, async (err, author) => {
              if (err) {
                console.log(err);
              } else {
                blog.author = author.name;
                console.log('this is the blogs' + blog);
                blogList.push(blog);
                console.log('array length 1 is ' + blogList.length);
              }
            });
          });
          console.log('array length 2 is ' + blogList.length);
          console.log(blogList);
          res.render('home', { blogs: blogList });
        }
      });
    });

Hello im trying to get all my blogs from DB and change the author
name from another collection from the database then rendering them to
ejs file however the page renders before the array is filled up

app.get('/', async (req, res) => {
      let blogList = new Array(); 
      await Blog.find({}, function (err, foundBlog) { 
        console.log(foundBlog);
        if (err) {
          console.log(err);
        } else {
          foundBlog.forEach(async (blog) => {
            await Author.findById(blog.author, async (err, author) => {
              if (err) {
                console.log(err);
              } else {
                blog.author = author.name;
                console.log('this is the blogs' + blog);
                blogList.push(blog);
                console.log('array length 1 is ' + blogList.length);
              }
            });
          });
          console.log('array length 2 is ' + blogList.length);
          console.log(blogList);
          res.render('home', { blogs: blogList });
        }
      });
    });

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

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

发布评论

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

评论(2

过潦 2025-01-17 15:53:10

请使用博客中作者的引用。换句话说,建立博客与作者的关系。然后您的后端应如下​​所示:

app.get('/', async (req, res) => {
      
         const blogs=await Blog.find({}).populate('author').exec(); // field name author is being populated.
      
          res.render('home', { blogs });
        }
    
    );

const BlogSchema=new mongoose.Schema({
 ... rest of fields,
 author:{
  type:ObjectId,
  ref:"Author" // here it is model name 
 }
});

现在使用此代码,您的整个作者对象将嵌入到每个博客中,如下所示:

blog={
  name:"",
  author:{
    name:"Abc",
  }
}

您可以通过访问 blog.author.name 轻松获取作者详细信息。

Please use ref of Author in Blog. In other words make a relation of blog with author. And then your backend should be as follows:

app.get('/', async (req, res) => {
      
         const blogs=await Blog.find({}).populate('author').exec(); // field name author is being populated.
      
          res.render('home', { blogs });
        }
    
    );

const BlogSchema=new mongoose.Schema({
 ... rest of fields,
 author:{
  type:ObjectId,
  ref:"Author" // here it is model name 
 }
});

With this code now your whole author object will be embedded in each blog like:

blog={
  name:"",
  author:{
    name:"Abc",
  }
}

And you can easily get author details by accessing blog.author.name.

嗼ふ静 2025-01-17 15:53:10

对于您的查询,您可以在集合中的 Mongo Schema 中探索 populate。

const BlogSchema = new mongoose.Schema({
 author:{
  type:ObjectId,
  ref:"Author" //here it is model name 
 }
})
const Blog = mongoose.model("Blog", BlogSchema )

const AutherSchema = new mongoose.Schema({
 _id:{
  type:ObjectId
 }
})
const Auther = mongoose.model("Auther", AutherSchema)

 app.get('/', async (req, res) => {
      
 const blogs = await Blog.find({}).populate('author').exec(); // field name author is being populated.
         res.send(blogs)
        }
    )

for your query, you can explore populate in your Mongo Schema in the collection.

const BlogSchema = new mongoose.Schema({
 author:{
  type:ObjectId,
  ref:"Author" //here it is model name 
 }
})
const Blog = mongoose.model("Blog", BlogSchema )

const AutherSchema = new mongoose.Schema({
 _id:{
  type:ObjectId
 }
})
const Auther = mongoose.model("Auther", AutherSchema)

 app.get('/', async (req, res) => {
      
 const blogs = await Blog.find({}).populate('author').exec(); // field name author is being populated.
         res.send(blogs)
        }
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文