Я хочу сохранить только один продукт, используя один серийный номер, но не могу? Когда я сохраняю установку с продуктом, регистрируется весь массив продуктов. Как мне это сделать?
Это моя схема Proreg
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);
Это схема установки, где serialno будет именем свойства, которое сохранит только один продукт, полученный из схемы proreg.
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
);
Сначала я создаю Proreg, чтобы объяснить, как сохранить данные в Proreg
.
//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" }));
});
Это маршруты установки
// 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));
});
//And this is the routes where I access only the products using the serialno by bringing both the models Installation and product
router.post("/selectone", (req, res) => {
Installation.find()
.then(inst => {
//Take the exact serialno
Proreg.find(
{ "products.serialno": req.body.serialno },
//Match the exact serialno
{
products: {
$elemMatch: { serialno: req.body.serialno }
}
}
)
.select("refno1")
.select("refno2")
.select("date")
.populate("customer", "customername")
.populate("customertype", "customertype")
.populate("department", "department")
//Populating the product nested array in object
.populate("products.oem", "companyname")
.populate("products.category", "category")
.populate("products.subcategory", "subcategory")
.then(inst => res.json(inst));
})
.catch(err => res.json(err));
});
Когда я беру продукт регистрации и сохраняю его, он сохраняет все продукты, которые идут вместе с ним. Я хочу взять конкретный серийно, который сохранит только один продукт. Как мне это сделать в req.body серийно?