Having trouble inserting data into two separate tables using javascript and mongojs. While I was able to successfully insert into a single collection by following the npm mongojs documentation, I couldn't find a solution for multiple collections on this platform. Any assistance would be greatly appreciated.
// var Delivered = require('../model/booking/booking').Delivered,
var Completed = require('../model/booking/booking').Completed,
express = require('express')
app = express(),
mongojs = require('mongojs'),
db = mongojs('mongodb:coolection',["completeds", "delivereds","deliveredhistory"]);
function route(app){
app.post('/shipper/completed',function(req, res){
var item_id = req.body.item_id
var id = mongojs.ObjectId(item_id);
db.delivereds.findAndModify({
"query":{ "_id": id },
"remove": false
},
function(err,data) {
if (data) {
db.completeds.insert(data,function(err,data){
if(err){
res.json({
status:400,
message:"Failed to mark as complete",
err:err
})
}
else{
res.json({
status:200,
message:"Job successfully completed",
data:data
});
}
});
// Inserting into another collection 'deliveredhistory'
db.deliveredhistory.insert(data,function(err,data){
if(err){
console.log("Insertion into deliveredhistory failed");
}
else{
console.log("Data inserted into deliveredhistory successfully");
}
});
}
else {
console.log("fake number")
res.json({
status: 403,
message:'This load is not available for accepting'
})
}
})
})
}
module.exports.route = route;
I'm still struggling with inserting data into another collection called 'deliveredhistory'. Can someone please provide guidance?
I attempted the following code snippet but it didn't work:
(db.completeds&&db.deliveredhistory)insert(data,function(err,data){