'use strict'
const mongoose = require('mongoose')
const productSchema = new mongoose.Schema ({
name: String,
image: String,
brand: { type: mongoose.Schema.Types.ObjectId, ref: 'Brand' }
})
const brandSchema = new mongoose.Schema ({
name: String,
image: String
})
const Product = module.exports = mongoose.model('Product', productSchema)
const Brand = module.exports = mongoose.model('Brand', brandSchema)
exports.postProduct = (req, res, next) => {
let newProduct = new Product(req.body)
newProduct
.save()
.then(() => {
res.json({
data: req.body
})
})
.catch((err) => {
return next(err)
})
}
{
"name": "Shapka",
"image": "image.jpg",
"brand": "5a33fae7b35cea5a408144c8"
}
exports.getProduct = (req, res, next) => {
Product
.find({})
.populate('brand')
.then((data) => {
res.json({
data
})
})
.catch((err) => {
return next(err)
})
}
{
"_id": "5a3426188cf9a12feaff9e5b",
"name": "shapka",
"image": "jdsnjdnskd",
"__v": 0
}