1

我正在开发这个 Node.js 应用程序,我发现在处理 MongoDb 数据库中的数组和子文档时遇到了一些困难。

我需要添加这么多子文档,而以这种形式,它不能。有什么帮助吗?

这是我的模型文件:

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

//schéma d'u devis
var productDevisSchema = new Schema({       
    article : String,
    marque : String,
    qte : Number,
    budget : Number,            
});

var devisSchema = new Schema({
    title: {type: String},
    description : String,
    createdOn: {type: Date, default: Date.now},
    createEnd: {type: Date},
    state : Boolean,
    userid: {type: Schema.Types.ObjectId , ref: 'Provider'},
    appelOffreid: {type: Schema.Types.ObjectId , ref: 'AppelOffre'},
    productdevis: [productDevisSchema]
});

module.exports = mongoose.model('Devis', devisSchema);

这是我的控制器功能:

exports.createDevis = function(req, res){
    var devis = new Devis({
        title : req.body.title,
        description : req.body.description,
        createEnd: req.body.createEnd,
        state : req.body.state,
        userid: req.body.userid,
        appelOffreid : req.body.appelOffreid,

        productdevis: [{
            article : req.body.article,
            marque : req.body.marque,
            qte : req.body.qte,
            budget : req.body.budget,
        }]
    });

    devis.save(function(err){
        if (err){
            return res.send(err);
        }

        res.send({message :'Devis ajoutée'});
    });
}
4

0 回答 0