2017-08-15 76 views
-1

我有以下schema貓鼬沒有任何反應,當我嘗試插入

module.exports = function (mongoose) { 
    var playlist = mongoose.Schema({ 
     title: String, 
     artist: String, 
     album: String, 
     time: Date 
    }); 

    return mongoose.model('playlist', playlist); 
}; 

然後我有以下代碼:

var PlayListModel = require('./schemas/playlist.schema.js')(mongoose); 
musicItem.time = new Date(); 
mongoose.models.playlist().save(musicItem).then(function (result) { 
    mongoose.find({title: musicItem.title}, function (err, doc) { 
     if(!err) 
     { 
      console.log(doc); 
     } 
     else 
     { 
      console.log(err); 
     } 
    }) 
}, function (err) { 
    console.log(err); 
}) 

但是沒有控制檯消息只有一個沒有錯誤以下警告當我啓動服務器:

`open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client 
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html 

任何人都可以告訴我什麼我做過錯了嗎?

+0

注意,回調的'find'回來了的文檔的數組,而不是一個對象(所以'docs',而不是'doc')和我不t認爲'mongoose'本身有一個'find'方法([PlayListModel does](http://mongoosejs.com/docs/queries.html))。 – Mikey

回答

1

你可以試試下面的代碼:

var PlayListModel = require('./schemas/playlist.schema.js')(mongoose); 

musicItem.time = new Date() 
var playListModel = new PlayListModel(musicItem); 
playListModel.save((err, playList) => { 

    // save data in playList 

    // find result 
    PlayListModel.find({title: musicItem.title}, (err2, result) => { 
     // result 
    }); 
));