2017-08-31 217 views
0

我在節點8.4.0上運行我的應用程序MongoDB 3.4,我無法讓貓鼬的人口工作。NodeJS - 貓鼬填充故障

我有兩個型號:

var mongoose = require('mongoose'); 
 

 
var Carrier = require('./Carrier.js'); 
 

 
var ValuationFormSchema = new mongoose.Schema({ 
 
    assignmentnumber: { 
 
     type: Number, 
 
     default: Number() 
 
    }, 
 
    assignmentpartnumber: { 
 
     type: Number, 
 
     default: Number() 
 
    }, 
 
    currency: { 
 
     type: String, 
 
     default: String() 
 
    }, 
 
    email: { 
 
     type: String, 
 
     default: String() 
 
    }, 
 
    iagree: { 
 
     type: Boolean, 
 
     default: Boolean() 
 
    }, 
 
    totalValue: { 
 
     type: Number, 
 
     default: Number() 
 
    }, 
 
    carrier: [{ 
 
     type: mongoose.Schema.Types.ObjectId, 
 
     ref: 'Carrier' 
 
    }], 
 
    createdAt: { 
 
     type: Date, 
 
     default: Date.now 
 
    }, 
 
    uuid: { 
 
     type: String, 
 
     default: String() 
 
    }, 
 
    submitted: { 
 
     type: Boolean, 
 
     default: false 
 
    }, 
 
    send: { 
 
     type: Boolean, 
 
     default: false 
 
    }, 
 
    template: String, 
 
    surveyvolume: Object, 
 
    agreement: Object 
 
}); 
 

 
module.exports = mongoose.model('ValForm', ValuationFormSchema);

var mongoose = require('mongoose'); 
 

 
var Item = require('./Item.js'); 
 

 
var CarrierSchema = new mongoose.Schema({ 
 
    name: { 
 
     type: String, 
 
     required: true 
 
    }, 
 
    items: [{ 
 
     type: mongoose.Schema.Types.ObjectId, 
 
     ref: 'Item' 
 
    }], 
 
    createdAt: { 
 
     type: Date, 
 
     default: Date.now 
 
    } 
 
}); 
 

 
module.exports = mongoose.model('Carrier', CarrierSchema);

,這是我的API

app.post('/api/valuationform/populate', function(req, res) { 
 
     var email = req.body.email; 
 
     ValForm.find({'email' : email},{'submitted' : false}) 
 
      .populate('Carrier')    
 
      .exec(function(error, docs) { 
 
       if (!error) { 
 
        console.log(JSON.stringify(docs)); 
 
        res.json(docs); 
 
       } else { 
 
        res.send('{ error: ' + error + ' }'); 
 
       } 
 
      }); 
 
    })

非但沒有載體的對象,我得到的對象的ID的。 每個閥值有多個運營商。

結果看起來是這樣的:

{ 
     "_id": "58d24ff9c195a72784108d85", 
     "assignmentid": 123441, 
     "__v": 0, 
     "send": false, 
     "uuid": "044da37c-8976-4f80-b103-d07c43a97af2", 
     "createdAt": "2017-03-22T10:20:41.260Z", 
     "carrier": [ 
      "58d24ff9c195a72784108d86", 
      "58d24ff9c195a72784108d8d" 
     ], 
     "totalValue": 40000, 
     "iagree": false, 
     "email": "[email protected]", 
     "currency": "SEK", 
     "assignmentpartnumber": 0, 
     "assignmentnumber": 0 
    } 

我想運營商被作爲對象返回不OBJECTID的。

我在這裏做錯了什麼?

+0

據我所知你必須在你自己的照顧。你必須在你的exec函數中手動提取它。 –

+0

對不起,這個愚蠢的問題,但這些載體對象存在與id 58d24ff9c195a72784108d86和58d24ff9c195a72784108d8d? – Helen

回答

0

也許嘗試在您的架構中定義的小寫字母carrier

ValForm.find({ email: email }, { submitted: false }).populate('carrier').exec(...);