mongodb数据库插入异常
发布于 10 年前 作者 wangxuq 3766 次浏览 最后一次编辑是 8 年前

错误如下,标签tags直接变成了post文章的内容,而tags的内容不见了,post变成null了,其中tags是一个数组。 { “_id” : ObjectId(“53828de1f6e4e37012b0114e”), “name” : “wangxu”, “time” : { “date” : ISODate(“2014-05-2 6T00:42:09.896Z”), “year” : 2014, “month” : “2014-5”, “day” : “2014-5-26”, “hour” : “2014-5-26-8”, “minut e” : “2014-5-26 8:42” }, “title” : “nodejs高级编程”, “tags” : “就是这样的优惠,你可以抗拒吗?”, “post” : null, “comments” : [ ] }

post.js模板存储部分如下:

function Post(name,title,tags,post){ this.name = name; this.title = title; this.tags = tags; this.post = post; } Post.prototype.save = function(callback){ var date = new Date(); //存储各种时间格式方便以后使用 var time = { date : date, year : date.getFullYear(), month : date.getFullYear()+’-’+(date.getMonth()+1), day : date.getFullYear()+’-’+(date.getMonth()+1)+’-’+date.getDate(), hour : date.getFullYear()+’-’+(date.getMonth()+1)+’-’+date.getDate()+’-’+date.getHours(), minute : date.getFullYear()+’-’+(date.getMonth()+1)+’-’+date.getDate()+’ ‘+date.getHours()+’:’+date.getMinutes() }; //要存入数据库的文档 var post = { name : this.name, time : time, title : this.title, tags : this.tags, post : this.post, comments : [] }; //打开数据库 mongodb.open(function(err,db){ if(err){ return callback(err); } //读取posts集合 db.collection(‘posts’,function(err,collection){ if(err){ mongodb.close(); return callback(err); } //将信息插入posts集合 collection.insert(post,{safe:true},function(err){ mongodb.close(); if(err){ return callback(err); } callback(null); }); }); }); };

post.ejs如下: <%include header.ejs%> <form method=“post”> <body> <label>标题</label><br/> <input type=“text” name=“title” id=“title”> <p> <label>标签(最多三个)</label><br/> <input type=“text” name=“tag1” /> <input type=“text” name=“tag2” /> <input type=“text” name=“tag3” /> <p> <label>内容</label><br/> <textArea type=“text” name=“post” id=“post” rows=“5” cols=“100”></textArea> <p> <button type=“submit”>发表</button> </body> </form> <%include footer.ejs%>

请各位给看看,不胜感激。谢谢。。。

2 回复

有人知道吗?请赐教,很捉急啊

终于解决了,实在太低级了。。。

回到顶部