There are two models, ProductModel and CategoryModel.
Purpose: When creating a product (ProductModel), assign a category to it, respectively, an array of products should be filled in the category collection, but the connection between them does not work. When creating a product (ProductModel), I output the response via json, it contains all fields except the category field, I need this field to be filled in too.
Product Model
import { Schema, model } from 'mongoose'
const ProductModel = new Schema({
productName: { type: String, required: true },
price: { type: Number, required: true },
preview: { type: String },
color: { type: String, required: true },
specs: {
images: [{ type: String }],
memory: { type: Number },
ram: { type: Number },
diagonal: { type: String },
},
category: {
name: String,
type: Schema.Types.ObjectId,
ref: 'Category',
},
})
export default new model('Product', ProductModel)
CategoryModel
import { Schema, model } from 'mongoose'
const CategoryModel = new Schema({
name: {
type: String,
required: true,
},
products: [
{
type: Schema.Types.ObjectId,
ref: 'Product',
},
],
})
export default new model('Category', CategoryModel)
The logic of the product creation route
async post(req, res, next) {
try {
// Get fields from client
let { productName, price, preview, color, specs, category } = req.body
// Looking for a category transferred from a client
const productCategory = await CategoryModel.find({ name: category })
console.log(productCategory)
// Creating product
const doc = new ProductModel({
productName,
price,
preview,
color,
specs,
category: productCategory._id,
})
// Save product
const product = await doc.save()
// Returning a response from the server to the client
return res.json(product)
} catch (error) {
console.log(error.message)
}
}
Here is what I send to the server and receive from it
Request:
{
"productName": "Air pods pro",
"price": 123,
"preview": "preview",
"color": "red",
"specs": {
"images": ["image1, image2, image3"],
"memory": 64,
"ram": 16,
"diagonal": "diagonal"
},
"category": "AirPods"
}
Response:
{
"productName": "Air pods pro",
"price": 123,
"preview": "preview",
"color": "red",
"specs": {
"images": [
"image1, image2, image3"
],
"memory": 64,
"ram": 16,
"diagonal": "diagonal"
},
"_id": "6609ad76341da85122e029d0",
"__v": 0
}
As you can see, there is no category field in the response, just like in the database, this field is missing in each product. And the Category collection, which has an array of products, is also not filled
I will attach screenshots below
In your
ProductModelschema thecategoryproperty has anameproperty given to it. That suggests you wantcategoryto be an Object with anameproperty. I pressume that was not your intention. You need to remove that so thatcategoryis just a property with a value of typeObjectId.To get the
product._idinto theproductCategory.productsarray you need to manually push it to the array like so: