Comparing Express User with Mongoose Foreign Key

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Comparing Express User with Mongoose Foreign Key



my mongoose model contains a foreign key for the model User.
If a put, post or delete request comes in, I would like to check if the primary ID of the currently authenticated user is the same as the foreign key.



If i log the id's they are exactly the same, can someone please tell me the correct way to go about this? Thanks in advance.



Model


mongoose.model(
'MyModel',
mongoose.Schema(
user:
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
,
...
)
)



Express Route


router.put('/models/:id', auth, (req, res, next) =>
MyModel.findById(req.params.id, (err, model) =>
if (err) return res.status(500).send(success: false, msg: 'Model not found');
if (req.user._id !== model.user) return res.status(500).send(sucess: false, msg: 'You did not create this model');

...success...
);
);









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard