2015-05-26 113 views
0

你好可以有人幫助我。我無法設置貓鼬模型場貓鼬設置referenceone

這裏我institute.js模型文件

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var instituteSchema = new Schema({ 
    name: {type: String, required: true}, 
    idApi: {type: String, required: true}, 
    country: { 
     type: Schema.ObjectId, 
     ref: 'Country' 
    }, 
    created_at: {type: Date, default: Date.now} 
}, {collection: 'qel_par_institute', versionKey: false}); 

instituteSchema.methods.findByIdApi = function (id, callback) { 
    return mongoose.model('Institute').findOne().where('idApi').equals(id).exec(callback); 
} 

instituteSchema.methods.findByCountry = function (id, callback) { 
    return mongoose.model('Institute').find().where('country.id').equals(id).exec(callback); 
} 

mongoose.model('Institute', instituteSchema); 

有sync.js堅持工作,除了我的部分不能手動設置國家裁判既不另一場

var instituteApi = new instituteModel({ 
    name: item.name, 
    idFromApi: item.id 
}); 
if (typeof item.country.id !== 'undefined') { 
    var country = new countryModel(); 
    country.findByIdApi(item.country.id, function(err, res) { 
     if (err) 
      console.log(err); 

     if (res) { 
      instituteApi.country = res._id; //setting this way doesn't work 
     } 
    }) 
} 
instituteApi.save(function(err) { 
    if (err) 
     console.log('something went wrong while saving!'); 
}); 
+0

oops got:全局變量instituteApi沒有在sync.js中定義 –

回答

0

無法設置它,因爲異步調用。切換回撥以Q module承諾。一切正常如想