I'm using node.js to upload values on a CSV sheet to a remote database.
The CSV sheet contains information on establishments and the services that they offer. Each row corresponds to a new establishment unless establishment information is null, if the establishment information is null then it just reads the service information and adds it to the last listed establishment (this way you can have multiple services for an establishment).
Reading this information into node is fine, it reads it in the correct order and separates it appropriately. The problem I'm having is using mongoose to upload the values.
New establishments are uploaded via mongoose.save (as they're a new entry) and the service updates are uploaded via mongoose.update.
New Establishment:
if (name != "" && name != lastName)
{
console.log("New Establishment added")
lastName = name;
var newEstab = new establishment({
name: name,
location: { lat: lat, long: long },
place_id: place_id,
username: username,
password: pass,
details: { bio: bio,
wait_time: wait,
opening_monday: opening_monday,
opening_tuesday: opening_tuesday,
opening_wednesday: opening_wednesday,
opening_thursday: opening_thursday,
opening_friday: opening_friday,
opening_saturday: opening_saturday,
opening_sunday: opening_sunday,
closing_monday: closing_monday,
closing_tuesday: closing_tuesday,
closing_wednesday: closing_wednesday,
closing_thursday: closing_thursday,
closing_friday: closing_friday,
closing_saturday: closing_saturday,
closing_sunday: closing_sunday
},
service: [{
name: sname,
price: sprice,
time_taken: time_taken,
service_category: scat,
}]
})
;
newEstab.save( (err, finish) => {
if (err) console.log("Error on Entry : " + err), reject(err)//; console.log("error" + err)
else console.log("Establishment Uploade") //, resolve(finish)//; console.log("went through" + book)
} )
}
Service Update:
console.log("Service update on " + lastName)
//console.log("work with me: " + sname + " " + sprice + " " + time_taken + " " + scat);
establishment.update(
{ name: lastName },
{"$push" :
{
'service' : { 'name' : sname,
'price' : sprice,
'time_taken' : time_taken,
'service_category' : scat,
},
}
},
// THE PROBLEM IS THE ORDER THEY"RE CARRIED OUT ^^^ PUSH HAPPENS BEFORE ESTABLISHMENT
function (err, model)
{
new Promise((resolve, reject) => {
if (err)
{
console.log("Error on Service update " + err)
reject(err)
} else {
console.log("Successful Service update ")
resolve(model)
}
})
})
For some reason, mongoose always runs the Update before the Insert, which means it tries to update an establishment that isn't there and can't complete the operation. There isn't a problem with the update function as if it's run separately afterwards it uploads correctly. It's just the order mongoose is running the functions.
Below is a screenshot of the terminal output display even though the insert functions are called before the update, they are executed in a different order. Terminal Output
If anyone could either help explain why mongoose does this or offer solutions to changing it- I'd be very appreciative.
Thanks for your time.
via Izaac Barratt
No comments:
Post a Comment