猫鼬/ Node js一对多关系不起作用

时间:2018-11-09 03:02:46

标签: node.js mongodb mongoose

下面是我的父母班

var mongoose = require("mongoose"),
    Schema = mongoose.Schema,
    relationship = require("mongoose-relationship");

const OrderSchema = new Schema({
    isPrime: {
        type: Boolean,
        default: false
    },
    deliveryDate: {
        type: Date,
        required: true
    },
    orderChannel: {
        type: String,
        required: true
    },
    orderDate: {
        type: Date,
        required: true
    },
    orderLabel: {
        type: String
    },
    orderStatus: {
        type: String,
        default: "NA"
    },
    orderTotal: {
        type: String,
        default: "NA"
    },
    orderType: {
        type: String,
        default: "NA"
    },
    shipDate: {
        type: Date
    },
    subOrderId: {
        type: String,
        default: "NA"
    },

    children1: [
        {
            type: Schema.ObjectId,
            ref: "ONLINE_ORDER_ITEMS"
        }
    ],
    children2: [
        {
            type: Schema.ObjectId,
            ref: "ONLINE_ORDER_CUSTOMERS"
        }
    ],
    children3: [
        {
            type: Schema.ObjectId,
            ref: "ONLINE_ORDER_SHIPMENTS"
        }
    ]
});

const Order = mongoose.model("ONLINE_ORDERS", OrderSchema);

module.exports = Order;

下面是我的孩子班级

var mongoose = require("mongoose"),
    Schema = mongoose.Schema,
    relationship = require("mongoose-relationship");

const OrderItemSchema = new Schema({
    productAsin: {
        type: String
    },
    productBoxNumber: {
        type: String
    },
    productColor: {
        type: String
    },
    productImagePath: {
        type: String
    },
    productName: {
        type: String
    },
    productOrderItemId: {
        type: String
    },
    productPrice: {
        type: String
    },
    productQuantity: {
        type: String
    },
    productSellerSku: {
        type: String
    },
    productVariation: {
        type: String
    },
    subOrderId: {
        type: String
    },
    orderId: {
        type: String
    },
    parent: [
        {
            type: Schema.ObjectId,
            ref: "ONLINE_ORDERS",
            childPath: "children1"
        }
    ]
});

OrderItemSchema.plugin(relationship, {relationshipPathName: "parent"});

const OrderItem = mongoose.model("ONLINE_ORDER_ITEMS", OrderItemSchema);

module.exports = OrderItem;

一个订单可以有多个订单项。我正在使用以下代码保存父母和孩子

const newOrder = new Order({
                    deliveryDate: e.LatestDeliveryDate,
                    orderChannel: "SportsVT-Amazon",
                    orderDate: e.PurchaseDate,
                    orderLabel: "NA",
                    orderStatus: "Pending",
                    orderTotal: e.OrderTotal.Amount,
                    orderType: e.OrderType,
                    shipDate: e.LatestShipDate,
                    isPrime: e.IsPrime,
                    subOrderId: e.AmazonOrderId
                });
newOrder.save()

下面是保存订单项

for (var i = 0 ; i < response.OrderItems.OrderItem.length;i++) {
                                    console.log("in here")
                                    const newOrderItem = new OrderItem({
                                        productAsin: response.OrderItems.OrderItem[i].ASIN,
                                        productBoxNumber: "NA",
                                        productColor: "NA",
                                        productImagePath: "NA",
                                        productName: response.OrderItems.OrderItem[i].Title,
                                        productOrderItemId: response.OrderItems.OrderItem[i].OrderItemId,
                                        productPrice: "NA",
                                        productQuantity: response.OrderItems.OrderItem[i].QuantityOrdered,
                                        productSellerSku: response.OrderItems.OrderItem[i].SellerSku,
                                        productVariation: "NA",
                                        subOrderId: shipment.subOrderId,
                                        parent: newOrder._id
                                    });
                                    promises.push(newOrderItem);

                                }
                                OrderItem.collection.insertMany(promises); 

在我的数据库中,我看到为父级添加了0个子级,但该子级又引用了父级。下面的图片用于父级收藏和子级收藏。 enter image description here

enter image description here

我的问题: 我在建立关系方面做错了什么。为什么父集合没有对子代的任何引用。 PS,当只有一项与父项关联时,它可以正常工作

0 个答案:

没有答案