Hi All ,

I am taking a full stack react course from an online learning platform, in 
which I am using node and express as the server, passport for 
authentication and MongoDB as the backend database. So I have a flow where 
when the user logs in a cookie is attached to the user, so on subsequent 
request from the user, the cookie is deserialized and the user record from 
the DB is stored in the req.user.

So I have a billing system which uses the stripe, where when the amount is 
charged 
he can create surveys (say $1 for 1 credit). Now I have the user model in 
the mongo db as

user: 
{ _id: abc,
googleId: xyz,
__v: 0,
credits: 0 },
Once when the user pays the amount the credits has to be updated and it has 
to be reflected in the front end in the header.

const keys = require('../config/keys')
const stripe=require('stripe')(keys.stripeSecretKey);

app.post('/api/stripe',async (req,res) => {
const charge = await stripe.charges.create({
amount: 500,
currency: "usd",
source: req.body.id, // obtained with Stripe.js
description: "$5 for 5 credits"
})

req.user.credits +=5;
const user = req.user.save(); 
});
};

My question is: How come changing the req.user and saving it updates the 
user model in the DB. As in the above code, I could not see any import for 
the mongoose user model?

I was expecting something like import the user model -> get the user id 
from req.user -> then use findById() -> to identify the user in MongoDB and 
then make the change in the credits.

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/5650fc07-d751-4f18-8535-cd3bd34cf4d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to