2017-09-05 43 views
0

我从web开发人员训练营创建这个博客应用程序和我在这个错误中创建新的博客窗体不发送任何数据返回到数据库和数据库将其保存为空。我正在使用mongodb,当我看到我的数据库时,它正在存储空对象。这里是app.jsnodejs窗体不发送任何数据req.body.blog

var express = require("express"), 
app = express(), 
bodyparser = require("body-parser"), 
mongoose = require("mongoose"); 

mongoose.connect("mongodb://localhost/restful", { 
useMongoClient:true 
}); 

app.set("view engine", "ejs"); 
app.use(express.static("public")); 
app.use(bodyparser.json()); 

// app.use(bodyparser.json({ type: 'application/vnd.api+json' })); 
app.use(bodyparser.urlencoded({extended:true})); 

//MONGOOSE MODEL CONFIG 
//================================= 
var blogSchema = new mongoose.Schema({ 
title: String, 
image: String, 
body: String, 
created: Date //{type: Date, default : Date.now} 

}); 

var blog = mongoose.model("blog" , blogSchema); 

// blog.create([{ 
//  title: "test post", 
// image : 
"https://imagejournal.org/wpcontent/uploads/2017/08/17140945161_586d5e98f7_o-600x300.jpg", 
//  body : "This is the first post" 
// }, 
// { 
// title: "second Post", 
// image: "https://images.pexels.com/photos/33109/fall-autumn-red- 
season.jpg?h=350&auto=compress&cs=tinysrgb", 
// body: "this is a second post" 
// }] 
//); 

//RESTful routes 
//========================================== 
//========================================== 

//Home 

app.get("/",function(req,res){ 
res.redirect("/blogs"); 
}) 
//NEW ROUTE 

app.get("/blogs/new",function(req,res){ 
    res.render("new"); 
}); 

//CREATE ROUTE 
app.post("/blogs", function(req,res){ 
//create blogs 
blog.create(req.body.blog, function(err, newblog){ 
if(err){ 
console.log("This is if error " + err); 
res.render("new"); 
} 
else{ 
//then redirect to the INDEX 
res.redirect("/blogs"); 
console.log("This is if no error " + req.body.blog + " "+ err); 
} 
}); 
}); 

//SHOW ROUTE 
app.get("/blogs/:id", function(req,res){ 
blog.findById(req.params.id, function(err,foundblog){ 
if(err){ 
    res.redirect("/blogs"); 
} 
else{ 
    res.render("show", {blog: foundblog}); 
} 
}); 
}); 

//INDEX ROUTE 
app.get("/blogs", function(req,res){ 
blog.find({}, function(err,blogs){ 
    if(err) 
     { 
      console.log("error"); 
     } 
    else 
     { 
       res.render("index", {blogs:blogs}); 

     } 
    }) 
}); 



//listening port 

app.listen(3000,function(){ 
    console.log("Blog app running"); 
}) 

而对于代码的形式TE代码是这样的:

<% include ./partials/header.ejs %> 

<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="blogtitle" placeholder="title"> 
</div> 
<div class="field"> 
    <label>Image</label> 
<input type="text" name="blogimage" placeholder="image"> 
</div> 
<div class="field"> 
<label>Body</label> 
<textarea name="blogbody" placeholder="Blog body goes here"></textarea> 
</div> 
<input class="ui inverted big olive button" type="submit" > 
</form> 

</div> 

<% include ./partials/footer.ejs %> 

现在控制台打印“这是,如果没有错误未定义空”

现在我做的是我通过

blog.create({ name: req.body.blogtitle, image:req.body.blogimage, body: 
req.body.blogbody} , function(err, newblog){...}); 

这似乎是工作,但如果我有架构中的许多参数,我应该hav e一个一个地宣布这个?在课程中,小马只输入了req.body.blog,并表示它包含所有的数据。

请帮帮我!

+0

你甚至在创建模型之前尝试记录'req.body',看看你实际得到了什么? – RaghavGarg

+0

我是console.logging req.body。博客 –

+0

请分享您正在关注的教程的链接。 – RaghavGarg

回答

0

根据你的HTML输入您将得到req.body像:

{ 
    blogtitle: 'your_blog_title', 
    blogimage: 'your_blog_image', 
    blogbody: 'your_blog_body' 
} 

因此,你有在下面的代码中为每个模型和每个领域使用它。

blog.create({ 
    name: req.body.blogtitle, 
    image: req.body.blogimage, 
    body: req.body.blogbody 
},function(err, newblog){...}); 

但如果你只想使用直接

// validate req.body.blog 
blog.create(req.body.blog, function(err, newblog){...}); 

,那么你就必须确定你的HTML类似model[field],下面的示例代码。

<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>Body</label> 
    <textarea name="blog[body]" placeholder="Blog body goes here"></textarea> 
    </div> 
    <input class="ui inverted big olive button" type="submit" > 
</form> 

请注意,为了直接使用req.body.blog;你必须把相同的字段(列名)给html输入。

+0

我尝试过使用博客[body],博客[image],博客[title],但它似乎没有正在处理req.body.blog,但在视频中它仍然通过格式模型[字段]显示。 ... –

+0

那么,请显示您的代码,以便我可以看到它可能有什么问题。你在说什么视频,我看不到任何与视频有关的输入。 – RaghavGarg

+0

没关系再次使用模型[field]方法来解决问题。 –

0

req.body的属性对应于<form>中输入字段的name属性。 req.body.blogundefined,因为您没有name="blog"的输入元素。

尝试console.log -ing req.body查看其属性,以便知道使用哪些属性。但根据您的<form>req.body将只有blogtitle,blogimageblogbody

+0

这是如果没有错误[对象对象] null使用req.body时会出现,但问题是它没有进入数据库和新帖子正在创建空帖子 –

0

我想你错过了输入名称。当你在你的服务器端代码上使用它时,它应该像blogtitleblogimageblogbodyreq.body填充像这样:

{ blogtitle: 'your_blog_title',blogimage: 'your_blog_image',blogbody: 'your_blog_body' } 

所以你必须抓住他们像req.body.blogtitle

//CREATE ROUTE 
app.post("/blogs", function(req,res){ 
//create blogs 
blog.create(req.body.blogtitle, function(err, newblog){ 
if(err){ 
    console.log("This is if error " + err); 
    res.render("new"); 
} 
else{ 
//then redirect to the INDEX 
    res.redirect("/blogs"); 
    console.log("This is if no error " + req.body.blogtitle + " "+ err); 
} 
}); 
}); 
+0

我不能传递三个输入blogtitle,blogimage和blogbody in猫鼬的创建功能它是givinig我错误未定义的标记。 –

+0

如果我通过bloggtitle它显示了我在表单中键入的内容,但同样没有输入数据库 –