使用未定义的变量创建新数组

时间:2017-11-17 18:43:37

标签: javascript node.js express

我正在创建一个博客Web应用程序。我目前将blogSchema设置为

var BlogSchema = new mongoose.Schema({
   title: String,
   image: String,
   body: String,
   created: 
         {type: Date, default: Date.now},
   author: {
      id: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "User"
      },
      username: String
   },
   comments: [
      {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Comment"
      }
   ]
});

module.exports = mongoose.model("Blog", BlogSchema);

我有以下

的帖子
<div class="ui main text container segment">
    <div class="ui huge header">New Blog</div>
    <form class="ui form" action="/blogs" method="POST">
        <div class="field">
            <label>Title</label>
            <input type="text" name="blog[title]" placeholder="title">
        </div>
        <div class="field">
            <label>Image</label>
            <input type="text" name="blog[image]" placeholder="image">
        </div>
        <div class="field">
            <label>Blog Content</label>
            <textarea name="blog[body]"></textarea>
        </div>
        <input class="ui violet big basic button" type="submit">
    </form>
</div>

在我的博客路线页面上,我将创建路线设为

router.post("/blogs", function(req, res){
    // create blog
    //req.body.blog.body = req.sanitize(req.body.blog.body);
    var title = req.body.title;
    var image = req.body.image;
    var desc  = req.body.body;
    var author= {
        id: req.user._id,
        username: req.user.username
    }
    var newBlogpost = {title: title, image: image, body: desc, author: author}
    Blog.create(newBlogpost, function(err, newBlog){
        if(err){
            res.render("blogs/new");
        } else {
            //then, redirect to the index
            console.log(newBlogpost)
            res.redirect("/blogs");
        }
    });
});

然而,当我在Web应用程序上登录并完成表单并在提交后查看我的命令行以返回console.log(newBlogpost)时,我得到:

{ title: undefined,
  image: undefined,
  body: undefined,
  author: { id: 5a0cbcc3d6c7070a7bb6c45e, username: 'cat' } 

我不确定为什么我正在创建的新变量数组将这三个变量视为未完成,并希望得到帮助。

0 个答案:

没有答案