How to reject first insert, If last insert get error (multiple inserts of collections)?


#1

I would like to reject if have insert problem with multiple collection insert at the same time.

CollectionA.insert(data, (error, _id)=>{
   CollectionB.insert(data);
})

If get error of CollectionB, the CollectionA is rejected too.
Please help me.


#2

Try this:

CollectionA.insert(data, (error, _id)=>{
if (_id) { 
// _id returns a value if the insert is successfully and error if not.

   CollectionB.insert(data, (error, _id2) => {
if (error) {
 // error here means CollectionB insert failed.
// Remove previous successfully insert _id from CollectionA

   CollectionA.remove({_id: _id});
// you should probably throw an Error here
}
});
}
})

I have used this method before to implement minor transaction.


#3

Thanks for your reply.