1

我正在努力将推文函数的响应值记录到控制台,但无论我做什么,即使发布了推文,该对象也会一直返回空。

const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();

const client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});

const tweet = (message, id = '0') => {
  let postRes = {};
  let status = {};
  if(id && id.length > 4) {
    status = {
      in_reply_to_status_id: id,
      status: message,
    };
  } else {
    status = {
      status: message,
    };
  }
  client.post('statuses/update', status)
  .then((tweet, response) => {
    console.log('id', tweet.id); // Tweet body.
    console.log('id_str', tweet.id_str); // Tweet body.
    console.log('text', tweet.text); // Tweet body.
    postRes.tweet = tweet.text,
    postRes.id = tweet.id_str;
    return postRes;
  })
  .catch((error) => {
    console.log('ERR');
    throw error;
  });
  // console.log('POSTRES', postRes);
  return postRes;
};

async function msg() {
  const tweeted = await tweet('this is_a__posts_async', '');
  console.log('TWEETED', tweeted);
  console.log('MESSAGE', tweeted.tweet);
  console.log('ID', tweeted.id);
}

msg();

在这里,我希望语句console.log('TWEETED', tweeted);返回一个包含两个元素的对象,即推文文本和发布推文的 id。然而,尽管它被包裹在一个异步函数中,但它返回的是空的。

4

3 回答 3

2

尝试将您的tweet函数转换async为如下所示的函数,或者您可以从tweet函数中返回整个 Promise。

async function tweet(message, id = '0') {
  let postRes = {};
  let status = {};
  let tweet;
  if(id && id.length > 4) {
    status = {
      in_reply_to_status_id: id,
      status: message,
    };
  } else {
    status = {
      status: message,
    };
  }
  try{
   tweet = await client.post('statuses/update', status)
  }
  catch(error){
     console.log('ERR: ', error)
     throw error
   }
   console.log('id', tweet.id); // Tweet body.
   console.log('id_str', tweet.id_str); // Tweet body.
   console.log('text', tweet.text); // Tweet body.
   postRes.tweet = tweet.text,
   postRes.id = tweet.id_str;
   return postRes;

};

async function msg() {
  const tweeted = await tweet('this is_a__posts_async', '');
  console.log('TWEETED', tweeted);
  console.log('MESSAGE', tweeted.tweet);
  console.log('ID', tweeted.id);
}

msg();

返还全部承诺。

const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();

const client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});

const tweet = (message, id = '0') => {
  let postRes = {};
  let status = {};
  if(id && id.length > 4) {
    status = {
      in_reply_to_status_id: id,
      status: message,
    };
  } else {
    status = {
      status: message,
    };
  }
  return client.post('statuses/update', status)
  .then((tweet, response) => {
    console.log('id', tweet.id); // Tweet body.
    console.log('id_str', tweet.id_str); // Tweet body.
    console.log('text', tweet.text); // Tweet body.
    postRes.tweet = tweet.text,
    postRes.id = tweet.id_str;
    return postRes;
  })
  .catch((error) => {
    console.log('ERR');
    throw error;
  });
  // console.log('POSTRES', postRes);
  // return postRes;
};

async function msg() {
  const tweeted = await tweet('this is_a__posts_async', '');
  console.log('TWEETED', tweeted);
  console.log('MESSAGE', tweeted.tweet);
  console.log('ID', tweeted.id);
}

msg();

谢谢,Bergi 指出范围问题。

于 2019-08-14T07:09:36.957 回答
1

嗯,我认为您在这里走在正确的轨道上,但是您需要在呼叫成功返回时解决承诺,如下所示:

const Twitter = require('twitter');
const dotenv = require('dotenv');
dotenv.config();

const client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});

const tweet = (message, id = '0') => {
  // no direct return value
  let status = {};
  if(id && id.length > 4) {
    status = {
      in_reply_to_status_id: id,
      status: message,
    };
  } else {
    status = {
      status: message,
    };
  }
  return client.post('statuses/update', status)
  .then((tweet, response) => {
    console.log('id', tweet.id); // Tweet body.
    console.log('id_str', tweet.id_str); // Tweet body.
    console.log('text', tweet.text); // Tweet body.
    postRes.tweet = tweet.text,
    postRes.id = tweet.id_str;
    // here we resolve with the successful promise to keep the chain intact
    return Promise.resolve(postRes);
  })
  .catch((error) => {
    console.log('ERR');
    throw error;
  });
};

async function msg() {
  // to handle any thrown errors use a try/catch here 
  try {
    const tweeted = await tweet('this is_a__posts_async', '');
    console.log('TWEETED', tweeted);
    console.log('MESSAGE', tweeted.tweet);
    console.log('ID', tweeted.id);
  } catch(error) {
    console.log(`Error during post: ${error}`);
  }
}

msg();

希望这有帮助!

于 2019-08-14T07:20:19.127 回答
1

Async/await 是 ES8 Javascript 中 Promises 的语法糖,但有时当你解决一个 Promise 时它会变得有点压倒性。最近,我整天都沉浸在试图适应它们的过程中。

您必须包装每个函数,async以尝试使用基于承诺的函数查看下面的代码以使用。

const dotenv = require('dotenv');
dotenv.config();

const client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY_TEST,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET_TEST,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY_TEST,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET_TEST
});

const tweet = (message, id = '0') => {
  let postRes = {};
  let status = {};
  if(id && id.length > 4) {
    status = {
      in_reply_to_status_id: id,
      status: message,
    };
  } else {
    status = {
      status: message,
    };
  }
  client.post('statuses/update', status)
  .then((tweet, response) => {
    console.log('id', tweet.id); // Tweet body.
    console.log('id_str', tweet.id_str); // Tweet body.
    console.log('text', tweet.text); // Tweet body.
    postRes.tweet = tweet.text,
    postRes.id = tweet.id_str;
    return postRes;
  })
  .catch((error) => {
    console.log('ERR');
    throw error;
  });
  // console.log('POSTRES', postRes);
  return postRes;
};

async function msg() {
  try{
     const tweeted = await tweet('this is_a__posts_async', '');
     console.log('TWEETED', tweeted);
     console.log('MESSAGE', tweeted.tweet);
     console.log('ID', tweeted.id);
     //it returns <Promise>
     return tweeted;
  }catch(error){
      console.log('Something went wrong', error);
      return;
  }

 } 
 //No need to call getTweeks
async function getTweets(){
  try{
    //Do what you want with this Object
   const tweet = await msg();
  }catch(error){
      console.log('Something went wrong', error);
      return;
  }
}

我想这会对你有所帮助。

于 2019-08-14T07:47:08.090 回答