I check if a post exists in table. If it does not, I insert it. So I need to make two async calls. Can I do this flat with promises?
var insertOrUpdateBuilding = (callback)=>{
// Check if building exists
db('buildings')
.where({externalId: buildingId})
.then(function(rows){
// Building exist, do nothing
if(rows){
callback ();
}
// Building does not exist. Insert it
if(!rows){
return db('buildings').insert({externalId: buildingId, name: req.body.name})
}
})
.then(function(promise){
})
.catch(function(err){
callback({message: 'Error looking up building', err: err})
})
};
I'm stuck. How do I proceed?