hi all i am studying node js for beginner making api from expres, i want to ask how to insert many data after cheking data exist and create data if data not exist? for example so i have data json in database
[{
"_id":1,
"product": t-shirt,
"brand": polo,
"price": ["3.335.000","4.000.000"],
"detail": {
"desc": "test1",
"size": "L"
}
},
{
"_id":2,
"product": t-shirt,
"brand": adidas,
"price": ["2.335.000","3.000.000"],
"detail": {
"desc": "test2",
"size": "L"
}
},{
"_id":3,
"product": t-shirt,
"brand": puma,
"price": ["5.000.000","9.000.000"],
"detail": {
"desc": "test3",
"size": "XL"
}
}]
and i have data, for post/insert to db
[{
"product": t-shirt,
"brand": polo,
"price": ["3.335.000","4.000.000"],
"detail": {
"desc": "test1",
"size": "L"
}
},
{
"product": t-shirt,
"brand": adidas,
"price": ["2.335.000","3.000.000"],
"detail": {
"desc": "test2",
"size": "L"
}
},{
"product": t-shirt,
"brand": puma,
"price": ["5.000.000","9.000.000"],
"detail": {
"desc": "test3",
"size": "XL"
}
},
{
"product": t-shirt,
"brand": nike,
"price": ["5.000.000","9.000.000"],
"detail": {
"desc": "test4",
"size": "XL"
}
},
{
"product": t-shirt,
"brand": aon,
"price": ["5.000.000","9.000.000"],
"detail": {
"desc": "test5",
"size": "XL"
}
}
]
UPDATE
THIS IS A CODE FROM server
var Gnew = require('../models/global/Globals.Prod.js');
var fs = require('fs');
module.exports = function(prouds) {
//CREATE
prouds.post('/create', function(req, res, err){
Gnew.find({}, function(err, proudcts){
if (err){
console.log(err)
}else{
// IF PRODUCTS === REQ.BODY THEN CREATE REQ.BODY NOT EXIST IN PRODUCT
// HOW TO CREATE THIS?
}
})
return prouds;
};
i post data using postman, and after post i get all data from DB for check data, and i want if exist he just ignore the same data and i want post data brand nike and aon
because brand of puma, adidas, and polo is exist
and how to create codition ?
oke i've try condition like this, i just want to get data not exist in db, why the output its same as like input? what is wrong with this code? hhe
req.body.forEach(function(itm1){
proudcts.forEach(function(itm){
// if ((itm1.judul === itm.judul) === null || undefined){
// fs.writeFile('cek.json', JSON.stringify(req.body));
// }else{
// fs.writeFile('cek1.json', JSON.stringify(req.body));
// }
if (!(itm1.judul === itm.judul)){
fs.writeFile('cek.json', JSON.stringify(req.body));
}else{
fs.writeFile('cek1.json', JSON.stringify(req.body));
}
})
})
thanks