小编lal*_*hav的帖子

填充对象 ID 数组

我的架构:-

var playlistSchema = new Schema({
name        : {type:String,require:true},
videos      : {type:[mongoose.Schema.Types.ObjectId],ref: 'Video'},
},{collection:'playlist'})

var Playlist = mongoose.model('Playlist',playlistSchema);
Run Code Online (Sandbox Code Playgroud)

我在数据库中有一些数据作为示例:-

{
"_id" : ObjectId("58d373ce66fe2d0898e724fc"),
"name" : "playlist1",
"videos" : [ 
    ObjectId("58d2189762a1b8117401e3e2"), 
    ObjectId("58d217e491089a1164a2441f"), 
    ObjectId("58d2191062a1b8117401e3e4"), 
    ObjectId("58d217e491089a1164a24421")
],
"__v" : 0
Run Code Online (Sandbox Code Playgroud)

}

视频的架构是:-

var videoSchema = new Schema({
name        : {type:String,required:true},
createdAt   : {type:String,default:new Date()},
isDisabled  : {type:Boolean,default:false},
album       : {type: mongoose.Schema.Types.ObjectId, ref: 'Album'}
},{collection:'video'})

var Video = mongoose.model('Video',videoSchema);
Run Code Online (Sandbox Code Playgroud)

现在为了获得播放列表中所有视频的名称,我正在尝试代码:-

 var playlistModel = mongoose.model('Playlist');
let searchParam = {};
searchParam._id = req.params.pid;
playlistModel.findOne(searchParam)
.populate('[videos]')
.exec(function(err,found){ …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

5
推荐指数
1
解决办法
7445
查看次数

User.findOne()不是函数

passport.use('local-signup', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
(req, email, password, done) => {
    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(() => {

    // find a user whose email is the same as the forms email
    // we are checking to see if the …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

2
推荐指数
2
解决办法
1万
查看次数

标签 统计

node.js ×2

javascript ×1

mongodb ×1

mongoose ×1