简体   繁体   中英

How do I access data from outside a function's scope?

I'm a new coder trying to learn the basics. For practice, I've built a little Node app that grabs the last tweet from a user's timeline and reverses it. Here's the code:

// REQUIRES
var dotenv = require('dotenv');
var Twitter = require('twitter');

// LOAD ENV VARIABLES
dotenv.load();

// MAKE A TWITTER CLIENT
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_TOKEN_KEY,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

// FUNCTION TO REVERSE A STRING
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}

// GRAB LAST TWEET FROM USER TIMELINE AND REVERSE IT
client.get('statuses/user_timeline', {screen_name: 'twitterapi', count: 1}, function(error, tweets, response) {
  if(error) throw error;
  // console.log(tweets);  // Last tweet object from timeline.
  var lastTweet = tweets[0].text; // Text from last tweet.
  var reverseTweet = reverse(lastTweet); // Last tweet reversed.
  console.log("\nYour last tweet was: " + lastTweet);
  console.log("\nYour last tweet reversed is: " + reverseTweet);
});

My question is: if I wanted to pass lastTweet or reverseTweet to another function outside the scope of that client.get() (for instance, to another text parsing function), how would I do that?

I've tried declaring the variables globally, but I think the async nature of the request/response screws it up. I'm definitely willing to learn about promises, callbacks, etc ( I understand zilch about them right now ) but I'm not sure if that's the right way to solve this particular problem.

Finally: if I'm not using the right vocabulary to ask the right questions, please let me know.

You could declare the variable at a higher scope level:

// REQUIRES
var dotenv = require('dotenv');
var Twitter = require('twitter');
var lastTweet = null;
var reverseTweet = null;

// LOAD ENV VARIABLES
dotenv.load();

...

Or probably best way in light of the fact that the client.get is an asynchronous call, is to pass lastTweet or reverseTweet as arguments to another function and call that function in the callback of client.get . For example:

// REQUIRES
var dotenv = require('dotenv');
var Twitter = require('twitter');

// LOAD ENV VARIABLES
dotenv.load();

// MAKE A TWITTER CLIENT
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_TOKEN_KEY,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

// FUNCTION TO REVERSE A STRING
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}

// GRAB LAST TWEET FROM USER TIMELINE AND REVERSE IT
client.get('statuses/user_timeline', {screen_name: 'twitterapi', count: 1}, function(error, tweets, response) {
  if(error) throw error;
  // console.log(tweets);  // Last tweet object from timeline.
  var lastTweet = tweets[0].text; // Text from last tweet.
  var reverseTweet = reverse(lastTweet); // Last tweet reversed.
  console.log("\nYour last tweet was: " + lastTweet);
  console.log("\nYour last tweet reversed is: " + reverseTweet);

  anotherFunction(lastTweet, reverseTweet);
});

function anotherFunction(lastTweet, reverseTweet)
{
    // Do something with vars here
}

You would have to declare reverseTweet or lastTweet outside of the client.get function.

You could do it as such:

// FUNCTION TO REVERSE A STRING
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}
var lastTweet = "";
var reverseTweet = "";

// GRAB LAST TWEET FROM USER TIMELINE AND REVERSE IT
client.get('statuses/user_timeline', {screen_name: 'twitterapi', count:     1}, function(error, tweets, response) {
  if(error) throw error;
  // console.log(tweets);  // Last tweet object from timeline.
  lastTweet = tweets[0].text; // Text from last tweet.
  reverseTweet = reverse(lastTweet); // Last tweet reversed.
  console.log("\nYour last tweet was: " + lastTweet);
  console.log("\nYour last tweet reversed is: " + reverseTweet);
});

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