简体   繁体   English

使用 mongoose 在 Mongo Atlas 中创建一个集合

[英]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。

working and saving to the collection after adding userNewUrlParser:添加 userNewUrlParser 后工作并保存到集合中:

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.

相关问题 使用mongoose在mongo中查询集合上的命名文本索引 - Query a named text index on a collection in mongo using mongoose 如何在Lambda函数中将Mongo Atlas数据库与Mongoose连接 - How to connect Mongo Atlas Database in Lambda function with Mongoose mongoose 通过 mongo atlas TypeError:无法读取未定义的属性“长度” - mongoose through mongo atlas TypeError: Cannot read property 'length' of undefined 如何使用 mongo 创建包含字段/变量/索引的集合 - How to create collection with fields/variables/indexes using mongo 使用 Mongo/Mongoose,为什么在将文档添加到现有集合时会创建一个全新的数据库? - Using Mongo/Mongoose, why is an entirely new database created when adding a document to an existing collection? 使用 node.js 和 mongoose 更新 Mongo DB 中的数据,即使集合不存在 - update data in Mongo DB even when collection does not exist using node.js and mongoose mongoose 有没有办法在找不到时不创建集合? - Is there a way in mongoose to not create a collection if not found? Mongo / Mongoose:Mongoose会自动在ObjectId类型上创建索引吗? - Mongo / Mongoose: Does Mongoose automatically create indexes on ObjectId types? 在猫鼬中使用现有集合的一部分 - Using a part of a existing collection in mongoose 使用 mongoose 在 Mongo DB 中搜索和过滤数据 - Search and filter data in Mongo DB using mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM