Node.js 猫鼬呼叫不返回
我得到了一个node.js应用程序,它连接到mongodb并尝试在集合中查找文档。不幸的是,通话从未结束 这是我的app.js的相关部分,我得到控制台日志Node.js 猫鼬呼叫不返回,node.js,mongodb,mongoose,passport.js,Node.js,Mongodb,Mongoose,Passport.js,我得到了一个node.js应用程序,它连接到mongodb并尝试在集合中查找文档。不幸的是,通话从未结束 这是我的app.js的相关部分,我得到控制台日志连接到mongodb,但在函数中表示完成用户。findOne不做任何事情/不调用然后 var mongoose = require("mongoose"); //connect to mongodb mongoose.connect(process.env.MONGO_DB_URI, { dbName: "newDb" }, () =>
连接到mongodb
,但在函数中表示完成用户。findOne
不做任何事情/不调用然后
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;
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:服务器选择在30000毫秒后超时
然后在删除useUnifiedTopology:true
mongonetworkerror无法连接到服务器
之前的代码没有给出。
这让我找到了这里的解决方案:。因此,基本上问题是我的IP地址已经更改,并且不再在mongodb中被列入白名单。您可以发布,您如何调用signInComplete()
函数以及通过passport调用signInComplete。我在问题中添加了代码。即使我在findOne()
的所有路径中执行return done()
操作,也不应该在findOne()
内部和save()
之后执行return done()。Iconsole.log()
每个路径都有,但没有一个路径记录在findOne()中。然后()
。
//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:'));