StephenGrider / FullstackReactCode

Companion repo to https://www.udemy.com/node-with-react-fullstack-web-development
1.39k stars 1.17k forks source link

req.user.save is not a function #69

Open Uhilasb opened 4 years ago

Uhilasb commented 4 years ago

Ive took every step that you did but yet im getting this error and i just cant find where is the problem?

const mongoose = require('mongoose'); const { Schema } = mongoose;

const UserSchema = new Schema({ googleId: String, credits: { type: Number, default: 0} });

mongoose.model('users', UserSchema);

//Route code const keys = require('../config/keys'); const stripe = require('stripe')(keys.stripeSecretKey); const User = require('../models/User');

module.exports = app => { app.post('/api/stripe', async (req, res) => { const charge = await stripe.charges.create({ amount: 500, currency: 'usd', description: '$5 for 5 credits', source: req.body.id }); req.user.credits += 5; const user = await req.user.save();

res.send(user);

}); };

//Index file const express = require('express'); const mongoose = require('mongoose'); const keys = require('./config/keys'); const bodyParser = require('body-parser'); const passport = require('passport'); require('./models/User'); require('./services/passport');

mongoose.connect(keys.mongoURI); const app = express();

app.use(bodyParser.json()); app.use(cookieSession({ maxAge: 30 24 8 8 1000, keys: [keys.cookieKey] })); app.use(passport.initialize()); app.use(passport.session());

require('./routes/authRoutes')(app); require('./routes/billingRoutes')(app);

const PORT = process.env.PORT || 5000; app.listen(PORT); console.log('The server is running!');

SudhirChavda19 commented 1 year ago

you solved this error ?? if solved then please tell me i also face same error