亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 mongoose 在 Mongo Atlas 中創建一個集合

使用 mongoose 在 Mongo Atlas 中創建一個集合

波斯汪 2022-11-03 15:17:59
我創建了一個新的貓鼬模型名稱“用戶”,并使用它創建了一個新對象以將新記錄保存到集合中。運行代碼后,我在 Mongo Atlas 中看不到任何集合。我使用 .once 檢查與 Mongo Atlas 的連接,但在運行并從 google auth 獲取我需要的配置文件后,我看不到在 Mongo 中制作的任何集合,也看不到我創建的對象中的任何記錄 - 使用未定義一個日志。index.jsconst 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 expressconst PORT = process.env.PORT || 5000;app.listen(PORT);用戶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 Schemaconst 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護照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'},更改為 .save() 后;并添加箭頭功能:console.log('profile Id',profile.id);        new User({ googleId: profile.id}).save(err => {            console.log(err)         });在 .save(err =>) 我在終端中得到空輸出。添加 userNewUrlParser 后工作并保存到集合中:mongoose.connect(keys.mongoURI,{ useNewUrlParser: true });在 save(err=>) 函數的終端中仍然得到一個 - null
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

save 是一個你必須調用它的函數

        new User({googleId:profile.id}).save(); //create a new instance of a User => and get the profile id , .scae => save to the MongoDB



查看完整回答
反對 回復 2022-11-03
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號