Is it possible that int he following code snippet, that your event will always be saved, even though the user ID is not found? I know that when you will later on have a login functionality etc,... this is very unlikely, but If you would do that check before hand then, you are 100% certain there ain't a gap.
I came across this due to the fact that I am working with some other relations where it is possible to inject an ID into an object where the original ID is not existing:
if (!updatedAthlete || !updatedRace) { throw new Error('Athlete or Race not existing'); } else { return resultRace.save().then(result => { console.log(updatedAthlete); updatedAthlete.results.push(result._id); updatedRace.athletes.push(result.athlete); updatedAthlete.save(); updatedRace.save(); return result; }); }
I have an ahtlete with an ID that is stored in the athlete model, and a race that is stored in the race model. When an athlete performs a race, a result will be created. this result needs an athlete id + race id which needs to be existent before we can save the raceresult, then the race result Id will be stored in the athlete model and the athlete will be stored in the particular race model.
The code above is working for me, but do you suggest other way of working?
(You can see my resolver createResult: in https://github.com/axelvuylsteke/fullstack-graphql-vue/blob/master/server/graphql/resolvers.js)
Hi,
Is it possible that int he following code snippet, that your event will always be saved, even though the user ID is not found? I know that when you will later on have a login functionality etc,... this is very unlikely, but If you would do that check before hand then, you are 100% certain there ain't a gap.
return event .save() .then(result => { createdEvent = { ...result._doc, _id: result._doc._id.toString() }; return User.findById('5c0f6dcde049d205fa2471dc'); }) .then(user => { if (!user) { throw new Error('User not found.'); } user.createdEvents.push(event); return user.save();
I came across this due to the fact that I am working with some other relations where it is possible to inject an ID into an object where the original ID is not existing:
if (!updatedAthlete || !updatedRace) { throw new Error('Athlete or Race not existing'); } else { return resultRace.save().then(result => { console.log(updatedAthlete); updatedAthlete.results.push(result._id); updatedRace.athletes.push(result.athlete); updatedAthlete.save(); updatedRace.save(); return result; }); }
I have an ahtlete with an ID that is stored in the athlete model, and a race that is stored in the race model. When an athlete performs a race, a result will be created. this result needs an athlete id + race id which needs to be existent before we can save the raceresult, then the race result Id will be stored in the athlete model and the athlete will be stored in the particular race model. The code above is working for me, but do you suggest other way of working? (You can see my resolver createResult: in https://github.com/axelvuylsteke/fullstack-graphql-vue/blob/master/server/graphql/resolvers.js)