How to publish a tweet using Twitter API with specific functions from another js file

余生长醉 提交于 2020-03-04 19:36:05

问题


This is my bot.js file... it consists of a simple twit publishing function with node.js

 const config = require('./config')
 const datasport = require('./botele')
 const twit = require('twit')
 const T = new twit(config)


 T.post(
    'statuses/update',
    { status: ' : '+ datasport.formattedRes },
   
    (err, data, response) => {
        if (err) {
            console.log("oof! Something went wrong!");
        } else {
             console.log("Tweet sent successfully!");
    
        }
      } 
  )

Then, in my botele.js file I have to..

var unirest = require("unirest");

var req = unirest("GET", "xxx");

req.query({
	"stats": "true",
	"events": "true"
});

req.headers({
	"x-x-host": "x",
	"x-x-key": "x"
});


req.end(function (res) {
	if (res.error) throw new Error(res.error);


	const formattedRes = res.body.results.map(singleObject =>{
		
		  return {
			   "homeName": singleObject.homeName,
				}
		});
		
		module.exports = {
			"formattedRes": formattedRes
		}
	


});

formattedRes returns exactly the variable I want: homeName like this:

homeName: MyhomeName

What I want is for my MyHomeName variable to appear on Twitter when I publish the twit! so I can work with it as:

{ status: ' :This is my home name call: '+ datasport.formattedRes + 'And yours? :) ' },

clearly gives me this on twitter post:

This is my home name call: undefined And yours? :)

some help!


回答1:


Your module.exports is inside a function. That is not the way you export something.
What you should do is export the function which generates formattedRes then run it to get your required data, and then use that data.



来源:https://stackoverflow.com/questions/59936472/how-to-publish-a-tweet-using-twitter-api-with-specific-functions-from-another-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!