Skip to content

req.user.save is not a function #69

Description

@Uhilasb

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!');

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions