我正在开发MEAN堆栈应用程序,我在两个模式“ Supplier”和“ Brand”之间进行了关联,我了解如何在模型文件(mongo)中进行操作,但是现在我不知道该在服务器中做什么侧面和客户文件(视图)。当我添加新的供应商以更新供应商集合以及品牌集合时,我需要它。我对MEAN Stack很新鲜,请帮助我,我真的迷路了...
supplier.js
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
Brand= require('../models/Brand');
var brandSchema = require('mongoose').model('Brand').schema;
const supplierSchema = mongoose.Schema({
suppliername: { type: String, required: true },
supplierPhone: { type: String },
supplierEmail: { type: String },
supplierAddress: { type: String },
brandSup: { type: brandSchema , ref:'Brand'}
});
module.exports = mongoose.model('Supplier', supplierSchema);
brand.js
const mongoose = require('mongoose');
const brandSchema = mongoose.Schema({
brandName: { type: String, required: true },
});
exports.brandSchema = brandSchema;
module.exports = mongoose.model('Brand', brandSchema);
suppliers.js
const express = require("express");
const Supplier = require('../models/supplier');
const router = express.Router();
router.post("", (req, res, next) => {
const supplier = new Supplier({
suppliername: req.body.suppliername,
supplierPhone: req.body.supplierPhone,
supplierEmail: req.body.supplierEmail,
supplierAddress: req.body.supplierAddress,
brandSub: req.body.brandSup,
});
supplier.save().then(createdSupplier => {
res.status(201).json({
message: 'supplier added successfully!',
supplierId: createdSupplier._id
});
});
});
router.get("", (req, res, next) => {
Supplier.find().then(documents => {
res.status(200).json({
message: 'suppliers fetched successfully!',
suppliers: documents
});
});
});
router.delete("/:id", (req, res, next) => {
Supplier.deleteOne({ _id: req.params.id }).then(result => {
console.log(result);
res.status(200).json({ message: "supplier Deleted!" });
});
});
module.exports = router;