猫鼬调用不 return

mongoose call does not return

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

这是我的 app.js 的相关部分,我得到了控制台日志 connected to mongodb,但是在函数 signInCompleteUserS.findOne 执行 nothing/does不调用 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
));

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

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

//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: Server selection timed out after 30000 ms

然后删除 useUnifiedTopology: true

mongonetworkerror failed to connect to server

前面的代码没有给出。 这让我找到了解决方案:。所以基本上问题是我的 IP 地址已经更改并且它不再在 mongodb 中列入白名单。