首页 > 解决方案 > Passport JS 不会将新模式保存到数据库?

问题描述

我正在尝试将 passport.js 与 GoogleStrategy 一起使用来授权用户使用我正在构建的 Web 应用程序。然后我想使用 mongoose 将用户存储在 MongoDB 中。

// Use the GoogleStrategy within Passport.
//   Strategies in passport require a `verify` function, which accept
//   credentials (in this case, a token, tokenSecret, and Google profile), and
//   invoke a callback with a user object.
passport.use(
    new GoogleStrategy({
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret,
        callbackURL: '/google/redirect'
    }, (accessToken, refreshToken, profile, done) => {
        console.log(profile);
        console.log(profile.displayName);
        console.log(profile.id);
        new Models.User({
            username: profile.displayName,
            googleID: profile.id,
            memberOf: null
            //console.log('got this far');
        }).save(console.log('got to the save')).then((newUser) => {
            console.log(newUser+'was created');
        });
        console.log('came down here');
    })
)

那是我对护照猫鼬的实施。从 Google 获取配置文件后,我会尝试将该用户的 displayName 和 googleID 存储在我想要保存到我的数据库中的用户架构中。我不确定我做错了什么,但是当我运行这个程序时,我看到所有console.log的输出到终端**except**,我希望有人可以帮助我解决这个问题。console.log(newUser+'was created');.then()

标签: javascriptnode.jsmongodbmongoosepassport-google-oauth

解决方案


更新:尝试在您的方法中添加一个错误处理程序,mongoose.connect()以便您可以查看您的应用程序是否成功连接到您的数据库。这就是我的问题。


推荐阅读