0

我有 express 2.* 的工作示例,但现在我正在转向版本 3.*。问题是使用 facebook 进行身份验证,这会导致一些问题。一切正常,直到everyauth 向facebook 发出GET 请求,然后返回错误:

500 Error: WARNING: You are trying to access the attribute/method configured by `findUserById`, which you did not configure. Time to configure it.

这是 app.js 代码

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , config = require('./conf/config.js')
  , user = require('./routes/user')
  , file = require('./routes/file')
  , http = require('http')
  , path = require('path')
  , everyauth = require('everyauth');

var UserService = require(__dirname + '/db/mongodb/UserService').UserService;

everyauth.facebook
  .appId(config.fb.appId)
  .scope('email')
  .fields('id,name,email')
  .appSecret(config.fb.appSecret)
  .findOrCreateUser(function(session, accessToken, accessToExtra, fbUserMetadata) {
    var promise = this.Promise();
    UserService.findOne({ email: fbUserMetadata.email }, function(err, user) {
      if (!user) {
        // Try to register user
        fbUserMetadata['accessToken'] = accessToken;
        UserService.save(fbUserMetadata);
        user = fbUserMetadata;
      }
      return promise.fulfill(user);
    });
    console.log(fbUserMetadata);
    return promise;
  })
  .findUserById(function(userId, cb) {
    console.log('findByUserId called');
    UserService.findOne({ id: userId }, function(err, user) {
      return cb(err, user);
    });
  })
  .redirectPath('/');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.compress());
  app.use(express.json());
  app.use(express.urlencoded());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(everyauth.middleware(app));
  app.use(app.router);
  app.use(require('less-middleware')({ src: __dirname + '/public' }));
  app.use(express.static(path.join(__dirname, 'public')));
});

everyauth.debug = true;
//everyauth.helpExpress(app);

app.configure('development', function(){
  app.use(express.errorHandler());
});

//console.log(everyauth.facebook);

app.get('/', routes.index);
app.get('/users', user.list);

app.get('/file', file.index);
app.post('/upload', file.upload);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

要解决此问题,请添加以下行:

everyauth.everymodule.findUserById(function(userId, cb) {
    console.log('findByUserId called');
    UserService.findOne({ id: userId }, function(err, user) {
      return cb(err, user);
    });
  });
4

1 回答 1

1

调用findUserByIdoneveryauth.everymodule而不是 on everyauth.facebook

everyauth.everymodule.findUserById(function(userId, cb) {
    console.log('findByUserId called');
    UserService.findOne({ id: userId }, function(err, user) {
        return cb(err, user);
    });
});
于 2012-12-08T21:49:28.353 回答