Javascript 请求在控制台中显示为对象,但在推送到数组时打印为字符串
我正在构建一个简单的express应用程序,它的功能是让用户可以对帖子发表评论。我想把评论和帖子/用户链接起来,这样我就有了一个所有3个的模式,我正在处理这些关系。 以下是注释模式中的相关部分:Javascript 请求在控制台中显示为对象,但在推送到数组时打印为字符串,javascript,mongodb,rest,express,mongoose,Javascript,Mongodb,Rest,Express,Mongoose,我正在构建一个简单的express应用程序,它的功能是让用户可以对帖子发表评论。我想把评论和帖子/用户链接起来,这样我就有了一个所有3个的模式,我正在处理这些关系。 以下是注释模式中的相关部分: const mongoose = require('mongoose') const Schema = mongoose.Schema const commentSchema = new Schema({ comment: String }) const Comment = mongoose.m
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const commentSchema = new Schema({
comment: String
})
const Comment = mongoose.model('Comment', commentSchema)
module.exports = Comment
和后模式
comments: [
{
type: Schema.Types.ObjectId,
ref: 'Comment'
}
]
})
const Album = mongoose.model('Album', albumSchema)
module.exports = Album
据我所知,这里一切正常
这是我在展示页面上提交的表格
<form action="/albums/<%=album.id%>/comments" method="POST">
<label for="comment">Share</label>
<textarea name="comment" id="comment" cols="30" rows="5"></textarea>
<button>Share</button>
<% for (let comment of album.comments) { %>
<p>Comment: <%=comment%>
</p>
<% } %>
</form>
并发布评论路线(也在展示页面上)。表单主体在控制台中显示为对象,但仅打印为id字符串
Album.findById(
req.params.id,
(err, foundAlbum) => {
const albumComment = new Comment(req.body)
albumComment.save()
foundAlbum.comments.push(albumComment)
foundAlbum.save()
res.redirect(`/albums/${req.params.id}`)
})
})
Album.findById(
req.params.id,
(err, foundAlbum) => {
const albumComment = new Comment(req.body)
albumComment.save()
foundAlbum.comments.push(albumComment)
foundAlbum.save()
res.redirect(`/albums/${req.params.id}`)
})
})