relu
relu

Reputation: 353

To post a tweet using npm package Twitter in node.js (not working)

I would like to post a tweet using npm package Twitter in node.jsenvronment. I am able to post a simple text tweet when I'm using client.post method as shown in the documentation here. The problem is when I try to obtain the status object using promise.then(), I couldn't post. The output of my promise is status object which is a simple url. Also, I do not receive any error or request response for my code below.

const Twitter = require('twitter'); 
const config = require('./config.js'); 
// passing client details from config file to new T class 
const T = new Twitter(config); 

T.post('statuses/update', getData(url).then((data)=>{
    let status = JSON.stringify(data);
    console.log(status)
    return status
}), function(error, tweet, response) {
    if(!error){
        console.log("tweet successfully sent", tweet.text);
    }
    else (error);
 });
// console.log(status)
// "http://example.com/"

I believe that I am doing some mistake in passing data from my promise object.Can someone help me correct this? Thank you.

Upvotes: 0

Views: 513

Answers (1)

Ashish Modi
Ashish Modi

Reputation: 7770

The problem is you are trying to do asynchronous opertion while passing a paramater so which eventually sends promise. You can restructure your code like this as twitter package also supports promise.

const Twitter = require('twitter'); 
const config = require('./config.js'); 
// passing client details from config file to new T class 
const T = new Twitter(config); 

getData(url)
 .then(data => JSON.stringify(data))
 .then(status => T.post('statuses/update', {status :status}))
 .then(tweet => console.log("tweet successfully sent", tweet.text))
 .catch(err => console.log(err));

Hope this helps.

Upvotes: 3

Related Questions