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

req.user.save is not a function #69

Open
Uhilasb opened this issue May 7, 2020 · 1 comment
Open

req.user.save is not a function #69

Uhilasb opened this issue May 7, 2020 · 1 comment

Comments

@Uhilasb
Copy link

Uhilasb commented May 7, 2020

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
Copy link

SudhirChavda19 commented Apr 5, 2023

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

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

2 participants