Home  >  Q&A  >  body text

node.js - 数据总是保存不进去

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...

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

高洛峰高洛峰2712 days ago671

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:18:40

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:18:40

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:18:40

    Data cannot be retrieved or saved

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:18:40

    You should save less on the poster item

    reply
    0
  • PHPz

    PHPz2017-04-17 15:18:40

    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

    reply
    0
  • Cancelreply