无法读取未定义的 属性 'name' - mongoose、express、nodeJS、ejs
Cannot read property 'name' of undefined - mongoose, express, nodeJS, ejs
我将如何从对象 ID 中获取用户名?我目前正在使用 populate 将用户数据从一个模式拉到另一个模式,它同时返回用户 ID 和名称,但我只想显示名称。我尝试在视图中使用 post.submittedby.name,但我一直收到 'name is undefined' 错误。我也试过将它设置为一个变量但同样的错误。
下面是我的数据在页面上的显示方式。
理想情况下,我想说 Posted By: user2
以下文件
Post 型号
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now,
required: true,
},
submittedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
comments: {
type: String,
default: 'Other info goes here',
}
})
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
用户模型
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
index.js
router.get('/dashboard', ensureAuthenticated, (req, res) => {
Post.find()
.populate({ path: 'submittedBy', select: 'name' })
.then((result) => {
res.render('dashboard', {
posts: result,
user: req.user,
})
})
.catch((err) => {
console.log(err)
})
})
首页page/dashboardview.ejs
<div class="forumView">
<h2>All Posts</h2>
<% if (posts.length > 0) {%>
<% posts.forEach(post => { %>
<h3 class="title"> <%= post.title %></h3>
<p class="body"> <%= post.body %> </p>
<p class="body"> Posted by: <%= post.submittedBy %> </p>
<% }) %>
<% } else { %>
<p>There are no posts to display...</p>
<% } %>
</div>
我认为发生的事情是当您将结果传递给模板时,那些 submittedBy
子文档被转换为字符串。所以试试这个:
router.get('/dashboard', ensureAuthenticated, (req, res) => {
Post.find()
.populate({ path: 'submittedBy', select: 'name' })
.lean() // <- This will give you plain JSON objects as opposed to mongoose docs.
.then((result) => {
result.forEach(i => i.submittedBy = i.submittedBy.name) // <- This will override the submittedBy prop with the actual name vs an object
res.render('dashboard', {
posts: result,
user: req.user,
})
})
.catch((err) => {
console.log(err)
})
})
此后,无需更改模板代码。
免责声明:我没有对此进行测试。
我将如何从对象 ID 中获取用户名?我目前正在使用 populate 将用户数据从一个模式拉到另一个模式,它同时返回用户 ID 和名称,但我只想显示名称。我尝试在视图中使用 post.submittedby.name,但我一直收到 'name is undefined' 错误。我也试过将它设置为一个变量但同样的错误。
下面是我的数据在页面上的显示方式。
理想情况下,我想说 Posted By: user2
以下文件
Post 型号
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now,
required: true,
},
submittedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
comments: {
type: String,
default: 'Other info goes here',
}
})
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
用户模型
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
index.js
router.get('/dashboard', ensureAuthenticated, (req, res) => {
Post.find()
.populate({ path: 'submittedBy', select: 'name' })
.then((result) => {
res.render('dashboard', {
posts: result,
user: req.user,
})
})
.catch((err) => {
console.log(err)
})
})
首页page/dashboardview.ejs
<div class="forumView">
<h2>All Posts</h2>
<% if (posts.length > 0) {%>
<% posts.forEach(post => { %>
<h3 class="title"> <%= post.title %></h3>
<p class="body"> <%= post.body %> </p>
<p class="body"> Posted by: <%= post.submittedBy %> </p>
<% }) %>
<% } else { %>
<p>There are no posts to display...</p>
<% } %>
</div>
我认为发生的事情是当您将结果传递给模板时,那些 submittedBy
子文档被转换为字符串。所以试试这个:
router.get('/dashboard', ensureAuthenticated, (req, res) => {
Post.find()
.populate({ path: 'submittedBy', select: 'name' })
.lean() // <- This will give you plain JSON objects as opposed to mongoose docs.
.then((result) => {
result.forEach(i => i.submittedBy = i.submittedBy.name) // <- This will override the submittedBy prop with the actual name vs an object
res.render('dashboard', {
posts: result,
user: req.user,
})
})
.catch((err) => {
console.log(err)
})
})
此后,无需更改模板代码。
免责声明:我没有对此进行测试。