2017-10-28 166 views
0

後3/4水平總是發現,貓鼬水平後,不填充3. 試圖與[email protected]^4.12.4貓鼬嵌套填入

var mongoose = require('mongoose'); 
 
var config = require('./config') 
 
var Promise = require('bluebird') 
 
mongoose.Promise = Promise 
 
mongoose.connect(config.dbURL, config.mongo.options) 
 
var NodeSchema = new mongoose.Schema({ 
 
    friend: { 
 
    type: mongoose.Schema.Types.ObjectId, 
 
    ref: 'Node' 
 
    }, 
 
    name: String 
 
}); 
 
var autoPopulateChildren = function(next) { 
 
    this.populate('friend'); 
 
    next(); 
 
}; 
 
NodeSchema 
 
    .pre('findOne', autoPopulateChildren) 
 
    .pre('find', autoPopulateChildren) 
 
var Node = mongoose.model('Node', NodeSchema) 
 
var user1 = new Node({ 
 
    name: 'A' 
 
}) 
 
var user2 = new Node({ 
 
    name: 'B' 
 
}) 
 
var user3 = new Node({ 
 
    name: 'C' 
 
}) 
 
var user4 = new Node({ 
 
    name: 'D' 
 
}) 
 
var user5 = new Node({ 
 
    name: 'E' 
 
}) 
 
user1.friend = user2._id 
 
user2.friend = user3._id 
 
user3.friend = user4._id 
 
user4.friend = user5._id 
 

 
Node.remove({}) 
 
    .then(Promise.all([user5, user4, user3, user2, user1].map(p => p.save()))) 
 
    .then(_ => Node.findOne({ 
 
    name: 'A' 
 
    })) 
 
    .then(r => console.log(r))

了以下.. A - >乙 - 「ç - >對象

但預期以下.. 甲 - >乙 - 「ç - > d - >電子

請幫助.. 在此先感謝..

回答

0

能否請您試試這個!?希望它能工作

user1.friend = user2 
user2.friend = user3 
user3.friend = user4 
user4.friend = user5 

Node.remove({}) 
    .then(Promise.all([user5.save(), user4.save(), user3.save(), user2.save(), user1.save()])) 
    .then(() => Node.findOne({ 
     name: 'A' 
    })) 
    .then(r => console.log(r))