Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从天气API中提取数据并在推特上发布_Javascript_Node.js_Json - Fatal编程技术网

Javascript 从天气API中提取数据并在推特上发布

Javascript 从天气API中提取数据并在推特上发布,javascript,node.js,json,Javascript,Node.js,Json,我正在设置一个推特机器人,通过推特发送城市的温度,你知道我的设置功能为什么不起作用吗 我试着换一个不同的API,但似乎什么都不起作用 console.log('starting twitter bot...') var Twit = require('twit'); var config = require('./config'); var T = new Twit(config); setup(); function setup() { loadJSON("http://ap

我正在设置一个推特机器人,通过推特发送城市的温度,你知道我的设置功能为什么不起作用吗

我试着换一个不同的API,但似乎什么都不起作用

console.log('starting twitter bot...')

var Twit = require('twit');

var config = require('./config');
var T = new Twit(config);


setup();


function setup() {

  loadJSON("http://api.apixu.com/v1/current.json?key=###############&q=Colombo", gotData);

}


function gotData(data) {
  console.log('Weather Data Retrieved...')

  var r = data.current[2];

  var tweet = {
    status: 'here is ' + r + ' temperature test '
  }

  T.post('statuses/update', tweet);

}
我得到这个错误:


引用错误:未定义loadJSON

是否使用p5.js?您应该注意,p5不会在节点服务器端运行,因为它依赖于访问窗口对象。因此loadJSON函数未定义

您可以使用XMLHttpRequest来检索数据

 function setup() {
  var request = new XMLHttpRequest()

  request.open('GET', 'https://api.apixu.com/v1/current.json?key=############################&q=Colombo', true)
  request.onload = function() {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response)

    if (request.status >= 200 && request.status < 400) {
      gotData(data)
    } else {
      console.log('error')
    }
  }

  request.send()
}
函数设置(){
var请求=新的XMLHttpRequest()
request.open('GET','https://api.apixu.com/v1/current.json?key=############################&q=Colombo",对)
request.onload=函数(){
//从这里开始访问JSON数据
var data=JSON.parse(this.response)
如果(request.status>=200&&request.status<400){
gotData(数据)
}否则{
console.log('错误')
}
}
请求发送()
}

我建议使用请求库获取天气条件,尤其是使用库,这使得读取API数据非常容易:

只要做:

npm install request
npm install request-promise-native
要安装,请执行以下操作:

const API_KEY = '7165..'; // Put your API key here
const Twit = require('twit');
const config = require('./config');
const rp = require('request-promise-native');

async function testWeatherTweet(location) {
    const options = {
        url: "http://api.apixu.com/v1/current.json",
        qs: { 
            key: API_KEY,
            q: location
        },
        json: true
    };
    let result = await rp(options);
    let condition = result.current.condition.text;
    let tweetText = `Conditions in ${location} are currently ${condition}, temperature is ${result.current.temp_c}°C.`;
    console.log("Sending tweet: ", tweetText);
    sendTweet(tweetText)
}

function sendTweet(text) {
    const T = new Twit(config);
    const tweet = {
        status: text
    }

    T.post('statuses/update', tweet);
}

testWeatherTweet('Colombo');

你的函数
loadJSON
在哪里?因为您收到的错误是,
loadJSON
不存在?我明白了,所以应该添加以下内容:
function loadJSON(path,success,error){var xhr=new XMLHttpRequest();xhr.onreadystatechange=function(){if(xhr.readyState==4){if(xhr.status==200){success(JSON.parse(xhr.responseText));}else{error(xhr);}}}};xhr.open('GET',path,true);xhr.send();}
我当然会尝试一下。谢谢你,我感谢你的帮助!太好了!我建议你从问题中删除API密钥,你可能不想让别人使用它!