简体   繁体   中英

Get latest tweets with nodejs and the twitter api

I am trying to get the latest 5 tweets with the twitter api and node / express, but I am not getting any result, when I call the url with .get

I would expect it to return a json object, when I am calling the url

Here is my code for the call, inspired from what I have found elsewhere and the answer here Get the list of latest tweets using node.js and twitterAPI

var ntwitter = require('ntwitter'),
credentials = require('./credentials.json'),
twitter,
tweets = {};

// set up our twitter objects
twitter = ntwitter(credentials);


tweets = twitter.get('/statuses/user_timeline.json', { screen_name: 'twitter', count: 5});

module.exports = tweets;

At the moment I am getting this error, when I start the server

    throw new Error('FAIL: INVALID CALLBACK.');
      ^
    Error: FAIL: INVALID CALLBACK.
    at Twitter.get (/home/vagrant/app/node_modules/ntwitter/lib/twitter.js:56:11)
    at Object.<anonymous> (/home/vagrant/app/twitter_stream.js:10:18)
    at Module._compile (module.js:456:26)
    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 Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/vagrant/app/server.js:7:19)
    at Module._compile (module.js:456:26)

And my server.js file where I require the twitter_stream.js file

var express     = require("express"),
path        = require("path"),
http        = require("http"),
bodyParser  = require("body-parser"),
tweets      = require("./twitter_stream.js"),
app         = express();


// Set the client folder as the default folder
app.use(express.static(__dirname + "/client"));

// Parse incomming json objects with bodyparser
app.use(bodyParser());

// Create the server
http.createServer(app).listen(3000);

// set up routes
app.get("/tweets.json", function (req, res) {
    // res.json returns the entire object as a JSON file
    res.json(tweets);
});

Just as an extra info: I am running node from a vagrant 64 bit box

Haven't been able to find much info on this, so I might be handling this completely wrong.

node.js is asynchronous. That means functions don't just return values.

//nope, asynchronous calls don't just return values.
tweets = twitter.get('/statuses/user_timeline.json', { screen_name: 'twitter', count: 5});

Use a callback:

twitter.get(
  '/statuses/user_timeline.json',
  { screen_name: 'twitter', count: 5}
  function(error, tweets) {
    //Looks, tweets are in here!
    //this function is called a "callback"
  }
);

Also, you can't do this:

module.exports = tweets;

Put functions in modules, not data loaded from the Internet. Use a function to expose that data to the rest of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM