Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 请求在控制台中显示为对象,但在推送到数组时打印为字符串_Javascript_Mongodb_Rest_Express_Mongoose - Fatal编程技术网

Javascript 请求在控制台中显示为对象,但在推送到数组时打印为字符串

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

我正在构建一个简单的express应用程序,它的功能是让用户可以对帖子发表评论。我想把评论和帖子/用户链接起来,这样我就有了一个所有3个的模式,我正在处理这些关系。 以下是注释模式中的相关部分:

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}`)
    })
})