使用Express JS和MongoDB发布后插入多个数据

时间:2019-04-20 09:22:04

标签: node.js mongodb express

im尝试使用Express无法以下列格式在mongodb中插入数据。 我需要在数据字段中输入多个产品和序列号。请帮忙!

[
    {
        "_id": "5cbabbd7545ac20f7c912e6a",
        "refno1": "REF1",
        "refno2": "REF2",
        "prodregdate": "2019-04-09T00:00:00.000Z",
        "data": [
            {
                "_id": "5cbabbd7545ac20f7c912e6b",
                "product": "5cb86b45cfafaa1860e29b2a",
                "serialno": "s123"
            },
            { // this data im not able to enter how to do it
                "_id": "5cbabbd7545ac20f7c912e6b",
                "product": "5cb86b45cfafaa1860e29b2a",
                "serialno": "s123"
            },
        ],
        "customer": {
            "_id": "5c98bb0a42207b16d8fbd3cf",
            "customername": "Raghav Update"
        },
        "customertype": {
            "_id": "5c7a1a1d4913fa08ac75c027",
            "customertype": "Government "
        },
        "__v": 0
    }
]

//我的模式

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

    const ProductRegistrationSchema = new Schema({
        //Product Details
        _id: { type: mongoose.Schema.Types.ObjectId },
        refno1: { type: String },
        refno2: { type: String },
        data: [{
            product: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "product"                      
            },
             //DATES
            //OEM
            oemwarrantyfrom: { type: Date },
           oemwarrantyto: { type: Date },
           //SERVICE PROVIDER
           warrantyfrom: { type: Date },
           warrantyto: { type: Date },
           serialno: { type: String },    
        }],

        prodregdate: { type: Date },
        //Details of Customer buying the product
        customer: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "customer" 
        },
        customertype: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "customertype" 
        },
        department: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "customersubdepartment" 
        },
         remarks: { type: String },
        entrydate: {
            type: Date,
            dafault: Date.now
        } 

module.exports = ProductRegistration = mongoose.model('productregistration', ProductRegistrationSchema);

//我的路线仅用于添加

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');

const Product = require("../../../models/Master/Products");
//importing the model of ProductRegistrationSchema
const ProdReg = require('../../../models/Entries/ProductRegistration');

//Creating a new ProductRegistration Data
router.post('/add', (req, res)=>{
    const newProdReg = new ProdReg({
        _id: new mongoose.Types.ObjectId(),
        refno1: req.body.refno1,
        refno2: req.body.refno2,
        prodregdate: req.body.prodregdate,
        data: {
          product: req.body.productid,
          oemwarrantyfrom: req.body.oemwarrantyfrom,
          oemwarrantyto: req.body.oemwarrantyto,
          warrantyfrom: req.body.warrantyfrom,
          warrantyto: req.body.warrantyto,
          serialno: req.body.serialno,
        },
        customer: req.body.customerid,
        customertype: req.body.customertypeid,
        department: req.body.customersubdepartmentid,
        remarks: req.body.remarks
        // deliverydate: req.body.deliverydate,
        // address: req.body.address,
        // assignedto: req.body.employeesid,
        // warrantyprovider: req.body.serviceproviderid,
        // oemwarrantyprovider: req.body.oemcompanyid,
        // warrantystartdate: req.body.warrantystartdate,
        // warrantyexpiredate: req.body.warrantyexpiredate,


    });
     newProdReg.save().then(prodreg => res.json(prodreg));

});

im无法在数据字段中输入2个产品和序列号。进入一个。

1 个答案:

答案 0 :(得分:1)

如果要插入从请求数据中获取的两个产品,请首先以正确的格式创建请求JSON。

例如,您的请求JSON应该采用以下格式:

{"refno1":"x", "refno2": "y", "prodregdate": "2019-04-19T18:30:00.000Z","data": [{"product": "product_1_object_id","oemwarrantyfrom":"2019-04-19T18:30:00.000Z", "oemwarrantyto": "2019-04-19T18:30:00.000Z","warrantyfrom":"2019-04-19T18:30:00.000Z", "warrantyto":"2019-04-19T18:30:00.000Z","serialno":"123" },{"product": "product_2_object_id","oemwarrantyfrom":"", "oemwarrantyto": "2019-04-19T18:30:00.000Z","warrantyfrom":"2019-04-19T18:30:00.000Z", "warrantyto":"2019-04-19T18:30:00.000Z","serialno":"456" }],"customersubdepartmentid":"departement_object_id","customerid":"customer_object_id","customertypeid":"customer_type_object_id","remarks":"anything"}

如果您使用的是POSTMAN,则可以在“原始”选项中尝试使用此JSON。

然后在您的代码中,它应该如下所示:

router.post('/add', (req, res)=>{
    const newProdReg = new ProdReg({
        _id: new mongoose.Types.ObjectId(),
        refno1: req.body.refno1,
        refno2: req.body.refno2,
        prodregdate: req.body.prodregdate,
        data: req.body.data,                      // This will be type array with two products details
        customer: req.body.customerid,
        customertype: req.body.customertypeid,
        department: req.body.customersubdepartmentid,
        remarks: req.body.remarks
    });
     newProdReg.save().then(prodreg => res.json(prodreg));

});

请使用我从您的模式中获取的JSON来匹配您的请求参数。