Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event is saved even though user is not defined #12

Open
axelvuylsteke opened this issue Aug 30, 2019 · 0 comments
Open

Event is saved even though user is not defined #12

axelvuylsteke opened this issue Aug 30, 2019 · 0 comments

Comments

@axelvuylsteke
Copy link

axelvuylsteke commented Aug 30, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant