如何访问猫鼬中的嵌套数组属性字段?

时间:2019-05-29 12:04:48

标签: node.js express mongoose

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

const AMCRegistrationSchema = new Schema({
	amcrefno: { type: String },
	amcregdate: { type: Date },
	customer: {
		type: mongoose.Schema.Types.ObjectId,
		ref: 'customer'
	},
	customertype: {
		type: mongoose.Schema.Types.ObjectId,
		ref: 'customertype'
	},
	department: {
		type: mongoose.Schema.Types.ObjectId,
		ref: 'customersubdepartment'
	},
	serviceprovider: {
		type: mongoose.Schema.Types.ObjectId,
		ref: 'serviceprovider'
	},
	amcstartdate: { type: Date },
	amcexpiredate: { type: Date },
	productrefno: {type: String },
	products: [
		{
			oem: {
				type: Schema.Types.ObjectId,
				ref: 'company'
			},
			category: {
				type: mongoose.Schema.Types.ObjectId,
				ref: 'productcategory'
			},
			subcategory: {
				type: mongoose.Schema.Types.ObjectId,
				ref: 'productsubcategory'
			},
			modelno: {
				type: String
			},
			serialno: {
				type: String,
				required: true
			}
		}
	]
	,
	remarks: { type: String },
	entrydate: {
		type: Date,
		dafault: Date.now
	}
});
module.exports = AMCRegistration = mongoose.model('amcregistration', AMCRegistrationSchema);

在AMC注册中,我有一个具有序列号的现有产品,也有一个具有新序列号的新产品。我正在通过另一种方式添加此新产品。要点:

  1. 首先,将通过此路线添加AMC

router.post("/add", (req, res) => {
  const newAMCReg = new AMCReg({
    amcrefno: req.body.amcrefno,
    amcregdate: req.body.amcregdate,
    customer: req.body.customerid,
    customertype: req.body.customertypeid,
    department: req.body.customersubdepartmentid,
    serviceprovider: req.body.serviceproviderid,
    amcstartdate: req.body.amcstartdate,
    amcexpiredate: req.body.amcexpiredate,
    productrefno: req.body.proregid,
    remarks: req.body.remarks
  });
  newAMCReg
    .save()
    .then(amc => res.json(amc))
    .catch(err => res.status(500).json({ msg: "Error in Adding AMC" }));
});

  1. 其次,将使用上面创建的amc的ID添加新产品,然后将检查serialno(productrefno)并将其与serialno(newproduct)进行比较。如果存在相同的serialno,则不允许添加新产品,否则它将允许。

//:id is the id of the amc
router.post("/product/:id", (req, res) => {  
  AMCReg.findById(req.params.id)

    .then(amcreg => {
      const newProduct = {
        oem: req.body.companyid,
        category: req.body.productcategoryid,
        subcategory: req.body.productsubcategoryid,
        modelno: req.body.modelno,
        serialno: req.body.serialno
      };

//THIS IS WHERE THE PROBLEM EXIST I CANNOT GET THE SERIAL NO OF THE NESTED PRODUCTREFNO 
      console.log(amcreg.productrefno.products.serialno);
      if (newProduct.serialno === amcreg.productrefno.products.serialno) {
	return res.json("You cannot enter the same serial number");
      } else {
        // Add to products array
        amcreg.products.push(newProduct);

        // Save
        amcreg.save().then(amcreg => res.json(amcreg));
      }
    })
    .catch(err => res.status(404).json({ amcregnotfound: "No amc reg found" }));
});

这里要添加的另一件事是,结果总是总是直接跳转到catch方法。我不知道如何以及为什么?有人可以帮我解决问题吗?我真的需要帮助。这是结果 result

0 个答案:

没有答案