[英]create a collection in Mongo Atlas using mongoose
I created a new mongoose model name 'users', and created a new Object using it to save a new record to the collection.我创建了一个新的 mongoose model 名称“用户”,并创建了一个新的 Object 使用它将新记录保存到集合中。
after running the code i cant see any collection in Mongo Atlas.运行代码后,我在 Mongo Atlas 中看不到任何集合。
I used.once to check the connection to the Mongo Atlas, but after running and getting the profile i needed from the google auth i cant see any collection made in Mongo and i cant see any record in the Object i created - getting an undefined using a log.我曾经使用.once 检查与 Mongo Atlas 的连接,但是在运行并从 google auth 获取我需要的配置文件后,我看不到在 Mongo 中制作的任何集合,也看不到我创建的 Object 中的任何记录 - 使用未定义一个日志。
index.js index.js
const express = require('express');
const mongoose = require('mongoose');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport'); //const passportconfig <=
const app = express();
const { Router } = require('express');
mongoose.connect(keys.mongoURI);
const connection = mongoose.connection;
connection.once('open',()=>{
console.log("MongoDB database connection established successfully");
});
require('./routes/authRoutes')(app); //authRoutes using app express
const PORT = process.env.PORT || 5000;
app.listen(PORT);
users用户
const mongoose = require('mongoose');
//const { mongoURI } = require('../config/keys');
//const Schema = mongoose.Schema;
const {Schema} = mongoose; //same as line 2 => tell to mongoose object that he have a property called Schema and use it in new object call Schema
const userSchema = new Schema({ //constructor i
googleId: String //user will have googleId for now
});
mongoose.model('users', userSchema);//our new collection User and will follow by the userSchema => only creating collection if it dosent exsits only
passport护照
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const keys = require('../config/keys');
const mongoose = require('mongoose');
const User = mongoose.model('users');
passport.use(new GoogleStrategy({
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback'
},
(accessToken, refreshToken, profile, done) => {
//console.log('accessToken',accessToken); //allow us to reach to google and show user permissions
//console.log('refreshToken',refreshToken); //allow us to refresh the accessToken
console.log('profile',profile);
new User({googleId:profile.id}).save(); //create a new instance of a User => and get the profile id , .scae => save to the MongoDB
console.log(User.googleId);
})
);//passport using new google oauth and handle it
after changing to.save();更改为.save() 后; andd adding arrow func:并添加箭头功能:
console.log('profile Id',profile.id);
new User({ googleId: profile.id}).save(err => {
console.log(err)
});
in.save(err =>) i get null output in the terminal. in.save(err =>) 我在终端中得到 null output。
mongoose.connect(keys.mongoURI,{ useNewUrlParser: true });
still getting a - null in the terminal on the save(err=>) func在 save(err=>) 函数的终端中仍然得到 - null
save is a function you have to call it like保存是 function 你必须这样称呼它
new User({googleId:profile.id}).save(); //create a new instance of a User => and get the profile id , .scae => save to the MongoDB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.