I'm trying to make a product model which has multi variants and this variants has multi packages any each package has different price and img for example:
Product Ram and CPU
Each one has a different variants, for eg: for Ram there is 16gb which is of price 50$ and 32gb which is of price 100%, for CPU there are i7 and i5 and each one of those has different price, how can i create a model for the scenario.
const ProductSchema = new Schema({
img:{ type:String, required:true },
name: [{
name:String,
}],
variants:[{
name:String,
target:[{
name:String,
img:String
}],
package:[{
name:String,
price:Number
}]
}]
})
I try to do this but it didn't worked
You can just create a model out of the schema and push to the package array the relevant data:
const ProductModel = new model('Product', ProductSchema);
...
// name, and variants are arrays so they default to empty arrays, only 'img' is required
const newProduct = new ProductModel({
img: 'http://balhblahimg.com/'
};
const variant = {
name: 'foo',
target: [],
package: []
};
...
...
variant.target.push({name: 'a', img: 'b'});
variant.package.push({name: 'i7', price: 54});
// Do manipulation on variant object, add to pacage and target...
// and push it to the newProduct
newProduct.name.push('boo');
newProduct.variants.push(variant);
newProduct.save();