【问题标题】:mongoose call does not return猫鼬电话不返回
【发布时间】:2020-04-07 18:36:12
【问题描述】:

我有一个 node.js 应用程序连接到 mongodb 并尝试在集合中查找文档。不幸的是,通话永远不会完成。

这是我的 app.js 的相关部分,我得到控制台日志 connected to mongodb,但在函数 signInComplete 中,UserS.findOne 什么都不做/不调用 then

var mongoose = require("mongoose");
//connect to mongodb
mongoose.connect(process.env.MONGO_DB_URI, { dbName: "newDb" }, () => {
  console.log("connected to mongodb");
});
const UserS = require("./models/user-model");

// Callback function called once the sign-in is complete
// and an access token has been obtained
async function signInComplete(iss, sub, profile, accessToken, refreshToken, params, done) {
  if (!profile.oid) {
    return done(new Error("No OID found in user profile."), null);
  }

  // Save the profile in user storage
  UserS.findOne({ msId: profile.oid }).then(currentUser => {
    if (currentUser) {
      //already have user
      console.log("user is:" + currentUser);
      done(null, currentUser);
    } else {
      //if not create new user
      new UserS({
        msId: profile.oid,
        profile,
        oauthToken
      })
        .save()
        .then(newUser => {
          console.log("new user created:" + newUser);
          done(null, newUser);
        });
    }
  });
}

我的用户模型

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema({
    msId:String,
    profile:Object,
    oauthToken:Object
});

const User = mongoose.model('user',userSchema,"users");

module.exports=User;

signInCompletepassport 调用

passport.use(new OIDCStrategy(
  {
    identityMetadata: `${process.env.OAUTH_AUTHORITY}${process.env.OAUTH_ID_METADATA}`,
    clientID: process.env.OAUTH_APP_ID,
    responseType: 'code id_token',
    responseMode: 'form_post',
    redirectUrl: process.env.OAUTH_REDIRECT_URI,
    allowHttpForRedirectUrl: true,
    clientSecret: process.env.OAUTH_APP_PASSWORD,
    validateIssuer: false,
    passReqToCallback: false,
    scope: process.env.OAUTH_SCOPES.split(' ')
  },
  signInComplete
));

我整天都在努力找出问题所在,但我在这里错过了什么?

【问题讨论】:

  • 你能发帖吗,你怎么也调用signInComplete()函数
  • signInComplete 通过护照调用。我在问题中添加了代码。
  • 难道你不应该在findOne()save() 之后做return done()
  • 即使我在findOne() 的所有路径中都执行return done() 也行不通。我 console.log() 每条路径也是如此,并且 none 记录在 findOne().then() 中。

标签: node.js mongodb mongoose passport.js


【解决方案1】:

我重新创建了整个项目并更改了连接数据库的方式

//Configure Database
mongoose.connect(process.env.MONGO_DB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

//Get the default connection
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

这样我终于得到了一个错误信息,首先:

MongoTimeoutError: 服务器选择在 30000 毫秒后超时

然后在删除useUnifiedTopology: true之后

mongonetworkerror 连接服务器失败

之前的代码没有给出。 它让我在这里找到了解决方案:Error at connecting to MongoDb Atlas Server。所以基本上问题是我的 IP 地址发生了变化,它不再在 mongodb 中列入白名单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-19
    • 2014-05-30
    • 2016-06-20
    • 2019-02-13
    • 2018-03-07
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多