问题
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