节点 - 将谷歌openid保存到mongodb

时间:2014-01-28 03:37:13

标签: node.js mongodb passport.js

我正在使用带有护照的nodejs,到目前为止,我已经成功地允许用户通过google openID登录我的应用程序,并使用以下内容:

passport.use( new GoogleStrategy({
        returnURL: 'http://localhost:22169/auth/google/return',
        realm: 'http://localhost:22169/'
    },
    function(identifier, profile, done) {
        // async verification
        process.nextTick( function() {
            profile.identifier = identifier;
            return done(null, profile);
        });
    }
));

我的下一步一旦我开始工作就是仅使用openID值并使用mongoose将其保存到mongodb中。这就是我认为应该工作的原因:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test'); 

var User = mongoose.model('User', { openId: String });

/*
  Some other stuff...
 */

passport.use( new GoogleStrategy({
        returnURL: 'http://localhost:22169/auth/google/return',
        realm: 'http://localhost:22169/'
    },
    function(identifier, profile, done) {
        User.findOrCreate({ openId: identifier }, function(err, user) {
            done(err, user);
        });
    }
));

但是当我运行节点并尝试通过谷歌登录时,我收到一条错误消息:

/Users/mporter/Sites/budgeter/b-server.js:42
        User.findOrCreate({ openId: identifier }, function(err, user) {
             ^
TypeError: Object function model(doc, fields, skipId) {
    if (!(this instanceof model))
      return new model(doc, fields, skipId);
    Model.call(this, doc, fields, skipId);
  } has no method 'findOrCreate'
    at Strategy.app.configure.app.use.express.session.secret [as _verify] (/Users/mporter/Sites/budgeter/b-server.js:42:14)
    at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/lib/passport-openid/strategy.js:219:16
    at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1063:12
    at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1203:12
    at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1265:7
    at Object.openid.loadAssociation (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:107:5)
    at _checkSignatureUsingAssociation (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1231:10)
    at _checkSignature (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1221:5)
    at _verifyAssertionAgainstProvider (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1184:3)
    at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1155:16

我认为它必须对User关键字做一些事情,但我认为我做得正确并创建了一个合适的猫鼬模型。任何人都愿意伸出援助之手。

1 个答案:

答案 0 :(得分:1)

如上所示,Passport期望您的模型使用findOrCreate方法,但此方法不会内置到mongoose模型中。试试mongoose findorcreate plugin,它会将findOrCreate方法添加到您指定的模型中。