NodeJS - Mongoose填补了麻烦

时间:2017-08-31 08:54:54

标签: node.js mongoose

我在Node 8.4.0 MongoDB 3.4上运行我的应用程序,我无法让Mongoose人群工作。

我有两种模式:

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。 每个valforms有多个载体。

结果如下:

{
        "_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": "a@b.com",
        "currency": "SEK",
        "assignmentpartnumber": 0,
        "assignmentnumber": 0
    }

我希望运营商作为不符合目标的对象返回。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

也许尝试使用模式中定义的小写carrier

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