【问题标题】:npm Twitter library not authenticating?npm Twitter 库不工作?
【发布时间】:2015-03-13 11:43:25
【问题描述】:

我有这个代码

var Twitter = require('twitter');

var client = new Twitter({

        consumer_key: process.env.TWITTER_CONSUMER_KEY,
        consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
        access_token_key: process.env.TWITTER_ACCESS_KEY,
        access_token_secret: process.env.TWITTER_ACCESS_SECRET,
});


client.post('status/update', {status: 'Test'}, function(error,params,response){
    if(error) throw;
    console.log(params);
    console.log(response);
});

但是当我运行 node tweet.js 时它总是给我这个

/Users/geraldpipes/projects/cmdtwitter/tweet:13
if(error) throw;
               ^
SyntaxError: Unexpected token ;
  at Module._compile (module.js:439:25)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:929:3

我 100% 确定我的环境变量是正确的,并且与他们在我的开发者 Twitter 上所说的完全一致。所以我不确定这里的问题是什么,甚至不知道如何解决它!

After fixing the syntax error and changing that line to     throw error;
the new problem is it is now this
    /Users/geraldpipes/projects/cmdtwitter/tweet.js:13
    if(error) throw error;
                    ^
    [object Object]

【问题讨论】:

  • 我也知道我没有任何东西被抛出,但即使我抛出错误它仍然不会正确

标签: node.js twitter


【解决方案1】:

试试client.post下面的代码

client.post('status/update', {status: 'Test'}, function(error,params,response){
    if(error) throw error;
    console.log(params);
    console.log(response);
});

其实你忘了throwerror。所以出现了语法错误。

【讨论】:

  • 我修复了这个问题,现在它只是说“/Users/geraldpipes/projects/cmdtwitter/tweet.js:13 if(error) throw error; ^ [object Object]”
【解决方案2】:

error 在您的情况下不是标准的 javascript Error 对象,因此不能以这种方式抛出,您可以检查 error 对象的值并查看它包含的内容,并通过 if 语句您可以处理您的操作,或者您可以通过发出 throw 'OMG!'; 来引发客户错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多