node.js - 数据总是保存不进去
高洛峰
高洛峰 2017-04-17 15:16:40
0
5
715
var mongoose = require("mongoose");

// 连接字符串格式为mongodb://主机/数据库名
mongoose.connect('mongodb://localhost/test');

var Schema = mongoose.Schema;
//骨架模版
var movieSchema = new Schema({
    doctor   : String,
    title    : String,
    language : String,
    country  : String,
    year     : Number,
    summary  : String,
    poster   : String,
    flash    : String
})
//模型
var Movie = mongoose.model('Movie', movieSchema);
//存储数据
var movie = new Movie({
    title: '黑衣人三',
    doctor: '史密斯',
    year: 2018,
    flash: 'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
    country: '美国',
    language: '英语',
    summary: '好片'
})
//保存数据库
movie.save(function(err) {
    if (err) {
        console.log('保存失败')
        return;
    }
    console.log('meow');
});

控制台出现下面的提示:
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/pr...

也没报错,但是数据就是插不进去

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(5)
Peter_Zhu

1) Add default port 27017 to the host address. That is, mongodb://localhost/test
is changed to mongodb://localhost:27017/test

2) Use code to test the connection to ensure connectivity. For example, add these sentences after mongoose.connect():

mongoose.connection.on('connected', function(){
    console.log('Connection success!');
});
mongoose.connection.on('error', function(err){
    console.log('Connection error: ' + err);
});
mongoose.connection.on('disconnected', function(){
    console.log('Connection disconnected');
});

3) Check which collection the data is stored in. The pitfall I encountered before was that if I use your phrase

var Movie = mongoose.model('Movie', movieSchema);

is written in a way that although you specify to save a collection named 'Movie', the collection actually saved may be a collection named 'Movies' (Mongoose automatically adds an s at the end) ). It sucks, but it does happen.

You can easily check it out by using commands such as show collections in Mongodb SHELL, or using visualization tools such as RoboMongo and MongoBooster.

巴扎黑

Add
mongoose.Promise = global.Promise;
before
mongoose.connect('mongodb://localhost/test');

Ty80

Data cannot be retrieved or saved

小葫芦

You should save less on the poster item

PHPzhong

The prompt is telling you that mongoose no longer implements promises built-in and you need to add a third-party promise plug-in yourself
The document.save you use may be a method that returns a promise
You can try changing it to Module.create

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template