Reputation: 3356
I am following full-stack react course by stephen grider. Everything looks good but after google social auth, I can't see new users
collection getting added to the mLab database.
I am using passport.js and mongoose.js.
Here is the relevant source code:
index.js
const express = require('express');
const mongoose = require('mongoose');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');
mongoose.connect(keys.mongoURI);
const app = express();
require('./routes/authRoutes')(app);
const PORT = process.env.PORT || 5000;
app.listen(PORT)
user.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
googleId: String
});
mongoose.model('users', userSchema);
passport.js
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');
const User = mongoose.model('users');
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback'
},
(accessToken, refreshToken, profile, done) => {
new User({ googleId: profile.id }).save();
console.log('accessToken',accessToken,'profile',profile)
}
)
);
Could anyone please let me know where I am making mistake because of which new collection is not getting created in the MongoDB database?
Upvotes: 0
Views: 802
Reputation: 217
If your DB password contain @
on mLab it won't work
Remove and try again it will work
Upvotes: 0
Reputation: 3356
The issue was in the user.js file. Defining Schema alone doesn't work. You will need to export it too.
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
googleId: String
});
module.exports = mongoose.model('users', userSchema);
Further, if anybody come across this type of error, please make sure you are using correct credentials(username and password) for the particular mongo database.
Upvotes: 1