1

就像您在这段代码中看到的那样,我想创建经典应用程序来注册属于用户的用户、产品和订单。

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/orders');

var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;



var ProductSchema = Schema({
    code: String,
    name: String,
    description: String
});

var Product = mongoose.model('Product', ProductSchema);

var OrderSchema = Schema({
    user: {type: ObjectId, ref:'User'},
    products: [{
        product: {type: ObjectId, ref: 'Product'},
        quantity: {type: Number}
    }]
});

var Order = mongoose.model('Order', OrderSchema);

var UserSchema = Schema({
    name: String,
    email: String,
    orders: [{type: ObjectId, ref: 'Order'}]
});

var User = mongoose.model('User', UserSchema);

/* 我将我的代码分成 4 个步骤,以使其更易于测试。对于每个测试,我必须设置步骤,并再次执行文件。节点 testfile.js */

var step = 1;

// 这里我创建产品并保存它。

if(step == 1){

    var product1 = new Product({
        code: '001',
        name: 'somename1',
        description: 'some description 1'
    });

    product1.save(function(e, product1){
        if(!e){
            console.log('Product 1 saved.');
        }
    });

    var product2 = new Product({
        code: '002',
        name: 'somename2',
        description: 'some description 2'
    });

    product2.save(function(e, product2){
        if(!e){
            console.log('Product 2 saved.');    
        }

    });



}else if(step == 2){

// 这里我正在创建客户

    // Creating the customer
    var Customer = new User({
        name: 'Erick',
        email: 'myemail@dot.com'
    });

    Customer.save(function(e, customer){
        if(!e){
            console.log('Customer '+customer.name+' saved.');
        }
    });
}else if(step ==3){

/* 我在这里创建订单,然后搜索产品并将它们添加到订单中,我不知道这是否正确,如果有人能解释我一些正确的方式,我将不胜感激 */

            // Creating the order for the user Erick
    User.findOne({name: 'Erick'}, function(e, user){
        if(!e){

            var ord = new Order({
                user: user._id
            });
            // Adding the first product
            Product.findOne({code: '001'}, function(e, productc1){
                if(!e){
                    ord.products.push({product: productc1._id, quantity: 2});

                    // Adding the second product
                    Product.findOne({code: '002'}, function(er, productc2){
                        if(!er){
                            ord.products.push({product: productc2._id, quantity: 3});

                            //console.log(ord);

                            // Now saving the order
                            ord.save(function(err, ord){
                                if(!err){
                                    console.log('Order saved.');
                                    // now i add the push the order to the user
                                    user.orders.push(ord._id);
                                    user.save(function(errr, usr){
                                        if(!errr){
                                            console.log('Order Added to the user');
                                            console.log(usr);
                                        }
                                    })                                  
                                }
                            })
                        }
                    });


                }
            });
        };
    });
}else if(step == 4){

/* 在这里,我试图用他的订单获取用户,并用属于的项目填充每个订单,但是当我尝试“populate('orders.products.product')”时,我收到错误“TypeError: CAnnot call method未定义的“路径”。

我应该如何填充?

*/

    User.findOne({name: 'Erick'}).populate('orders').exec(function(e, erick){
        if(!e){
            console.log(erick); 
        }       
    });
}
4

0 回答 0