0

我的代码如下

var express = require('express'),
    everyauth = require('everyauth'),
  util = require('util'),
  Promise = everyauth.Promise,
  users = require('./users');

everyauth.twitter
  .consumerKey('')
  .consumerSecret('')
  .findOrCreateUser(function(session, accessToken, accessTokenSecret, twitterUserData) {
    console.log('pre promise');
    var promise = new Promise();
   users.findOrCreateByTwitterData(twitterUserData, promise);
    return promise;
  })
  .redirectPath('/');

它打印'pre promise'然后崩溃如下

pre promise

/.../node_modules/everyauth/lib/modules/everymodule.js:348
  throw err;
        ^
TypeError: undefined is not a function
    at Object._findOrCreateUser ( .../test.js:12:19)
    at Object.exec ( .../node_modules/everyauth/lib/step.js:47:21)
    at  .../node_modules/everyauth/lib/stepSequence.js:26:38
    at [object Object].fulfill ( .../node_modules/everyauth/lib/promise.js:42:25)
    at  .../node_modules/everyauth/lib/stepSequence.js:29:23
    at [object Object].fulfill ( .../node_modules/everyauth/lib/promise.js:42:25)
    at  .../node_modules/everyauth/lib/modules/twitter.js:16:15
    at passBackControl ( .../node_modules/everyauth/node_modules/oauth/lib/oauth.js:359:11)
    at IncomingMessage.<anonymous> ( .../node_modules/everyauth/node_modules/oauth/lib/oauth.js:378:9)
    at IncomingMessage.emit (events.js:88:20)

用户看起来像:

var cradle = require('cradle');
var c = new cradle.Connection('...', 80, {
    auth:{username:'admin', password:'...'}
});
var users = c.database('users');

module.exports.findOrCreateByTwitterData = function(twitterUserData, promise) {
    console.log('finding');
    users.view('docs/twitterId', {key:twitterUserData.id_str}, function (err, doc) {
        if (err) {
            console.log('twitter error');
            return;
        }
        if (doc) {
            promise.fulfill(doc);
        }
        else {
            var doc = {

            };
        }
        c.database('users').save(doc, function (err, res) {
            if (err) {
                console.log('error with users');
                return;
            }
            promise.fulfill(doc);
        });
    });
}
4

2 回答 2

1

我对everyauth 0.2.32 有同样的问题。看起来everyauth.Promise 没有被导出,当您尝试实例化“未定义”类时应用程序崩溃

您可以检查是否 (Promise == 'undefined')

于 2012-05-03T16:26:48.997 回答
0

在everyauth 的最新版本中,您应该使用以下语法:

.findOrCreateUser(function(session, accessToken, accessTokenSecret, twitterUserData) {
    var promise = this.Promise();
    users.findOrCreateByTwitterData(twitterUserData, promise);
    return promise;
})
于 2012-05-16T22:00:31.960 回答