Cannot save one object from another reference document in mongoose?

时间:2019-05-31 11:41:38

标签: node.js express mongoose

I want to save only one product using one serialno but i cannot ? When I save the installation with the productregistrationid whole product array is coming. How do I do it?

This is the Installation Schema

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

const InstallationSchema = new Schema({
  installrefno: { type: String },
  installdate: { type: Date },
  serialno: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "proreg"
  },
  installedby: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "employees"
  },

  customer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customer"
  },
  customertype: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customertype"
  },
  department: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customersubdepartment"
  },
  contactperson: { type: String },
  contactno: { type: Number },
  address: { type: String },
  remarks: { type: String },
  filename: { type: String },
  installdoc: { type: String }
});

module.exports = InstallationDetails = mongoose.model(
  "installation",
  InstallationSchema
);

This is the Proreg schema

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

// Create Schema
const ProRegSchema = new Schema({
  refno1: {
    type: String
  },
  refno2: {
    type: String
  },
  date: {
    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"
  },

  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
      },
      warrantyfrom: {
        type: Date,
        default: "01-01-2019"
      },
      warrantyto: {
        type: Date,
        default: "01-01-2019"
      },
      oemwarrantyfrom: {
        type: Date,
        default: "01-01-2019"
      },
      oemwarrantyto: {
        type: Date,
        default: "01-01-2019"
      }
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = ProReg = mongoose.model("proreg", ProRegSchema);

This is the result of the proreg 1

1.1

Now you can see that the Proreg has two products and in the Installation when I save the data it is taking both the products which I do not want. I want that when the user(take for example) enter one serialno which is coming from proreg in the installation it should take only that particular product with respect to that serialno can someone help me out here please?

This is the installation routes

// Set The Storage Engine
const storage = multer.diskStorage({
  destination: "client/public/install/docs",
  filename: function(req, file, cb) {
    cb(
      null,
      file.fieldname + "-" + Date.now() + path.extname(file.originalname)
    );
  }
});

const upload = multer({
  storage: storage,
  limits: {
    fileSize: 1024 * 1024 * 5
  }
});
//Create a new Installation Details
router.post("/add", upload.single("installdoc"), (req, res, next) => {
  console.log(req.file);
  const newInstallationDetails = new InstallationDetails({
    installrefno: req.body.installrefno,
    installdate: req.body.installdate,
    serialno: req.body.productregistrationid,
    installedby: req.body.employeesid,
    customer: req.body.customerid,
    customertype: req.body.customertypeid,
    department: req.body.customersubdepartmentid,
    contactperson: req.body.contactperson,
    contactno: req.body.contactno,
    address: req.body.address,
    remarks: req.body.remarks,
    installdoc: req.file.path
  });
  newInstallationDetails.save().then(installation => res.json(installation));
});

This is how I create the proreg first add the proreg refno and all and after this I take the id of the proreg and add the product

//ADD THE PROREG FIRST
router.post("/", (req, res) => {
  const newProReg = new ProReg({
    refno1: req.body.refno1,
    refno2: req.body.refno2,
    date: req.body.date,
    customer: req.body.customerid,
    customertype: req.body.customertypeid,
    department: req.body.customersubdepartmentid
  });

  newProReg.save().then(proreg => res.json(proreg));
});
//TAKE THE ID OF THE PROREG AND ADD THE PRODUCT
router.post("/product/:id", (req, res) => {
  ProReg.findById(req.params.id)
    .then(proreg => {
      const newProduct = {
        oem: req.body.companyid,
        category: req.body.productcategoryid,
        subcategory: req.body.productsubcategoryid,
        modelno: req.body.modelno,
        serialno: req.body.serialno,
        warrantyfrom: req.body.warrantyfrom,
        warrantyto: req.body.warrantyto,
        oemwarrantyfrom: req.body.oemwarrantyfrom,
        oemwarrantyto: req.body.oemwarrantyto
      };
      // Add to products array
      proreg.products.push(newProduct);

      // Save
      proreg.save().then(proreg => res.json(proreg));
    })
    .catch(err => res.status(404).json({ proregnotfound: "No pro reg found" }));
});

0 个答案:

没有答案