Node.JS Express 4 – Mongoose不保存数据

我想用Express.JS 4和Bluebird在MongoDB中保存MongoDB中的数据。

我所做的就是这样 –

斌/ WWW

var mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); ....... ....... db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() {// successfully connected! console.log("Successfully Connected to Mongo-DB"); }); 

并在控制台中获取这个 –

 Successfully Connected to Mongo-DB` - So, MongoDB connected successfully 

车型/ post.js

 var mongoose = require('mongoose'); var postSchema = new mongoose.Schema({ created_by: String, //should be changed to ObjectId, ref "User" created_at: {type: Date, default: Date.now}, text: String }); module.exports = mongoose.model('Post', postSchema); 

app.js

 var Post_Data = require("./models/post"); .... .... router.get('/', function(req, res, next) { var Post = mongoose.model("Post"); var post = new Post({ created_by: ""+Math.random() }); console.log( Post.create(post) ); res.render( 'index', { title : 'Express', site_name : 'Our Site', layout : 'templates/layout' } ); }); 

之后,我在控制台中得到这个

 Promise { _bitField: 0, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _promise0: undefined, _receiver0: undefined } 

但是, 没有什么是保存的 ,这是一个certificate是 –

我发现这个 –

在这里输入图像描述

使用MongoBooster后

最新情况:

我的数据库configuration是这样的 –

 "MONGO_URI": "mongodb://localhost:27017/express_test", "MONGO_OPTIONS": { "db": { "safe": true }, "name":"express_test" } 

那么,任何人都可以请帮忙,为什么不保存任何东西?

预先感谢您的帮助。

.create()函数是new Model.save()的快捷方式。 您正试图.create一个Model的实例,而不是一个简单的Object。 请参阅在Mongoose的模型文档中构build文档以获取其快速示例。

Mongoose数据函数的返回只是未来运行asynchronous任务的承诺,日志logging在很大程度上毫无意义。 使用.then()等待承诺解决。

error handling从你的代码中丢失了,可能会引发一些错误。 使用.catch()进行承诺error handling。

 Post.create({ created_by: ""+Math.random() }) .then(function (result) { console.log('Saved' result) }) .catch(function (err) { console.error('Oh No', err) }) 

所有这些都可以通过callback来完成(比如Mongoose docco的例子),但承诺,特别是蓝鸟承诺更好。

我只是使用这个语法组合来创build和保存我的模型:

 var myPage = new LandingPage({ user:req.user, slug: req.body.slug, }).save(function(err,savedModel){ if(!err){ console.log(savedModel); } }); 

您正在导入模型时,您正在调用app.js模块中的错误模型

 var Post_Data = require("./models/post"); // <-- Post_Data model never used .... .... 

但在你的路由器实现中创build一个新的Post模型实例

 var Post = mongoose.model("Post"); // <-- different model var post = new Post({ created_by: ""+Math.random() }); 

您需要调用并使用正确的模型。 所以我build议你重写你的app.js模块来使用save()方法:

 var Post = require("./models/post"); // <-- import correct Post model .... .... router.get('/', function(req, res, next) { var post = new Post({ created_by: ""+Math.random() }); post.save().then(function(post) { console.log(post); // <-- newly created post res.render('index', { title: 'Express', site_name: 'Our Site', layout: 'templates/layout' }); }) .catch(function(err) { console.error('Oopsy', err); }); }); 

如果通过require将variables模式存储在variables中,则可以使用该variables。

 var Post_Data = require("./models/post"); 

所以可以使用new Post_Data不需要使用var Post = mongoose.model("Post"); new Post_Data var Post = mongoose.model("Post"); 因为你已经导出了这个模式module.exports = mongoose.model('Post', postSchema);

你可以试试这个:

 var Post_Data = require("./models/post"); router.get('/', function(req, res, next) { var post = new Post_Data({created_by: ""+Math.random()}); post.save(function(error, data) { if(error) { return res.status(500).send({error: 'Error occurred during create post'}); } return res.render('index',{ title : 'Express', site_name : 'Our Site', layout : 'templates/layout' }); }); }); 

所以,如果你正在通过调用new Post(values)来创build一个内存中的文档,你可以用post.save(cb);来保存它post.save(cb); 而不是'Post.create(post); , but I'm thinking that the underlying issue (though this isn't easy to be certain of based on the code you're showing) is that you're connecting with the MongoDB driver, rather than mongoose itself. Your , but I'm thinking that the underlying issue (though this isn't easy to be certain of based on the code you're showing) is that you're connecting with the MongoDB driver, rather than mongoose itself. Your数据库variables没有显示在你发布的代码中声明,所以我把它作为一个假设。

这就是说,如果我是正确的,你需要调用mongoose.connectmongoose.createConnection为了让Mongoose知道它连接到数据库并保存文档。 你可以通过现有的连接mongoose,所以如果你已经这样做,那么我为我的错误的假设道歉。