我是Node.js和Express的新手。我正在尝试完成Twitter的Everyauth的配置,并且试图访问服务器端的'res.user',根据Everyauth的文档要求配置.findByUserId。那就是我遇到麻烦的地方。您如何配置findByUserId?

这就是我得到的。目前,它正在创建一个新用户或登录他们,但我尚未正确设置.findByUserId,但仍可以访问服务器端所需的req.user。如果您还有其他有关如何优化此代码的建议,也请告诉我,因为我只是在学习。

代码编辑如下...

App.js

var express = require('express')
  , everyauth   = require('everyauth')
  , util        = require('util')
  , Promise     = everyauth.Promise
  , users       = require('./lib/users')
  , config      = require('./config.js')
  , twitter     = require('./lib/twitter')
  , games        = require('./lib/games')

  everyauth.twitter
    .consumerKey(config.twitter.consumerKey)
    .consumerSecret(config.twitter.consumerSecret)
    .findOrCreateUser( function (session, accessToken, accessTokenSecret, twitterUserMetadata) {
      var promise = this.Promise();
      users.findOrCreateUserbyTwitterData(twitterUserMetadata, accessToken, accessTokenSecret, promise)
      return promise;
    })
    .redirectPath('/');
  everyauth.everymodule.findUserById(function (userId, callback) {
      users.findById(userId, callback);
  });

var app = module.exports = express.createServer();
everyauth.helpExpress(app);
// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({secret: "FxT10477A93d54HJx5"}));
  app.use(everyauth.middleware());
  app.set('view engine', 'jade');
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

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

// Routes

app.get('/', function(req, res){
  console.log(req.user);
  res.render('index', { title: 'Zeitheist 2' });
});


Users.js

var twitter = require(__dirname + "/twitter");
var mongoose = require('mongoose');
var Schema = mongoose.Schema
  , ObjectId = Schema.ObjectId;

var userSchema = new Schema({
    user                  : ObjectId
    , id                  : Number
    , twitterId           : Number
    , twitterName         : String
    , profileImageUrl     : String
    , name                : String
    , dateCreated         : Date
    , session             : String
    , accessToken         : String
    , accessTokenSecret   : String
});

var conn = mongoose.createConnection('mongodb://localhost/zeitheist2_test');
var userModel = conn.model('User', userSchema);

this.findOrCreateUserbyTwitterData = function(twitterData, accessToken, accessTokenSecret, promise) {
  userModel.find({'twitterId' : twitterData.id}, function(err, users) {
     if(err) throw err;
     if(users.length > 0) {
       promise.fulfill(users[0]);
     } else {
       var user = new userModel();
       user.twitterId = twitterData.id
       user.twitterName = twitterData.screen_name
       user.profileImageUrl = twitterData.profile_image_url
       user.name = twitterData.name
       user.accessToken = accessToken
       user.accessTokenSecret = accessTokenSecret
       user.save(function(err) {
         if (err) throw err;
         twitter.follow(accessToken, accessTokenSecret);
         promise.fulfill(user);
       });
     }
   });
}
this.findById = function(userId, callback) {
  userModel.findOne({'_id' : userId}, function(err, user) {
    if(err) throw err;
    if(callback)
      callback(user);
    else
      return user;
  });
}


为了我能访问req.user,everyauth.everymodule.findUserById需要如何查找?在成功登录后登录req.user的默认路由中,它显示为“ undefined”

最佳答案

您的代码应如下所示:

everyauth.everymodule.findUserById(function (userId, callback) {
    userModel.findById(userId, callback);
});


在您的代码中,您从应该调用回调的函数返回值。请记住,节点中的大多数事物都是异步的,并使用回调返回涉及I / O的任何事物(如数据库查找)的结果。

关于node.js - 为everyauth配置findByUserId- Node ,express, Mongoose ,everyauth,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10388098/

10-10 00:09