Javascript 如何从另一个模型中获取和查找另一个模型值?
我希望找到用户模型值,并希望将该值存储在我当前的博客模型中Javascript 如何从另一个模型中获取和查找另一个模型值?,javascript,node.js,mongodb,mongoose,nosql,Javascript,Node.js,Mongodb,Mongoose,Nosql,我希望找到用户模型值,并希望将该值存储在我当前的博客模型中 exports.getAllBlogs = async (req, res) => { try { const blogs = await Blog.find() .sort({ created_at: -1 }) .populate({ path: "comments" }) .exec(); const myblogs = blogs.map(
exports.getAllBlogs = async (req, res) => {
try {
const blogs = await Blog.find()
.sort({ created_at: -1 })
.populate({
path: "comments"
})
.exec();
const myblogs = blogs.map(async blog => {
const user = await User.findOne({ _id: blog.author });
return (blog.authorName = user.name);
});
console.log(myblogs);
if (blogs.length === 0) {
return res.status(404).json({ message: "No Blogs Found" });
}
return res.status(200).json(myblogs);
} catch (error) {
console.log(error);
res.status(500).json({ error: "Something went wrong" });
}
};
但这是在回报我[承诺{}]
每件收藏品
如何实现这一点?有更好的方法吗?因为在映射中使用异步。这将返回一系列承诺 试试这个
const myblogs=wait Promise.all(
blogs.map(异步blog=>{
const user=await user.findOne({u id:blog.author});
blog.authorName=user.name;
返回博客;
})
);代码>您可以使用“填充”获取所有相关信息,而无需地图
exports.getAllBlogs=async(req,res)=>{
试一试{
const blogs=wait Blog.find({})
.sort({created_at:-1})
.populate(“作者”,“密码”)
.填充({
路径:“评论”,
填充:{
路径:“用户”,
选择:“-密码”
}
});
返回res.status(200).json(blogs);
}捕获(错误){
console.log(错误);
json({error:“出错了”});
}
};
这将给出如下示例结果:
[
{
"_id": "5e53b2896f41c765fc4defa1",
"title": "Blog2 Title",
"body": "Blog2 Body",
"author": {
"_id": "5e53b1726f41c765fc4def9c",
"email": "user1@gmail.com",
"name": "User1",
"created_at": "2020-02-24T11:20:18.343Z",
"updated_at": "2020-02-24T11:20:18.343Z",
"id": "5e53b1726f41c765fc4def9c"
},
"created_at": "2020-02-24T11:24:57.078Z",
"updated_at": "2020-02-24T11:24:57.078Z",
"__v": 0,
"comments": [
{
"_id": "5e53b34c6f41c765fc4defa5",
"body": "Comment3 (user2 on user1's blog2)",
"user": {
"_id": "5e53b1906f41c765fc4def9d",
"email": "user2@gmail.com",
"name": "User2",
"created_at": "2020-02-24T11:20:48.070Z",
"updated_at": "2020-02-24T11:20:48.070Z",
"__v": 0,
"id": "5e53b1906f41c765fc4def9d"
},
"blog": "5e53b2896f41c765fc4defa1",
"created_at": "2020-02-24T11:28:12.551Z",
"updated_at": "2020-02-24T11:28:12.551Z"
}
],
"id": "5e53b2896f41c765fc4defa1"
},
{
"_id": "5e53b26c6f41c765fc4def9f",
"title": "Blog1 Title",
"body": "Blog1 Body",
"author": {
"_id": "5e53b1726f41c765fc4def9c",
"email": "user1@gmail.com",
"name": "User1",
"created_at": "2020-02-24T11:20:18.343Z",
"updated_at": "2020-02-24T11:20:18.343Z",
"id": "5e53b1726f41c765fc4def9c"
},
"created_at": "2020-02-24T11:24:28.895Z",
"updated_at": "2020-02-24T11:24:28.895Z",
"__v": 0,
"comments": [
{
"_id": "5e53b2f86f41c765fc4defa3",
"body": "Comment1 (user2 on user1's blog1)",
"user": {
"_id": "5e53b1906f41c765fc4def9d",
"email": "user2@gmail.com",
"name": "User2",
"created_at": "2020-02-24T11:20:48.070Z",
"updated_at": "2020-02-24T11:20:48.070Z",
"id": "5e53b1906f41c765fc4def9d"
},
"blog": "5e53b26c6f41c765fc4def9f",
"created_at": "2020-02-24T11:26:48.506Z",
"updated_at": "2020-02-24T11:26:48.506Z"
},
{
"_id": "5e53b3246f41c765fc4defa4",
"body": "Comment2 (user3 on user1's blog1)",
"user": {
"_id": "5e53b1996f41c765fc4def9e",
"email": "user3@gmail.com",
"name": "User3",
"created_at": "2020-02-24T11:20:57.488Z",
"updated_at": "2020-02-24T11:20:57.488Z",
"id": "5e53b1996f41c765fc4def9e"
},
"blog": "5e53b26c6f41c765fc4def9f",
"created_at": "2020-02-24T11:27:32.305Z",
"updated_at": "2020-02-24T11:27:32.305Z"
}
],
"id": "5e53b26c6f41c765fc4def9f"
}
]
这是可行的,但现在我只获取名称,然后我更改了一些代码`const myblogs=await Promise.all(blogs.map(async blog=>{const user=await user.findOne({u id:blog.author});blog.author=user.name;return blog;})`但是它并没有返回值,而是返回相同的值。有没有更好的方法来实现这一点,比如查询或者其他可以获取每个博客作者姓名和id的方法?blog.authorName=user.name
不是相同的blog.author=user.name;返回博客代码>。可能是错了吧?您需要blog.**authorName**=user.name;返回博客代码>