使用api响应节点/角度发送数据

时间:2019-05-26 01:38:13

标签: node.js angular api express

我正在尝试向城市发送天气信息的响应电话。但是,城市信息来自另一个api调用,该调用将坐标传递给天气api。

我尝试将对象添加到天气api的结果中并发送整个结果失败

async function getLocation(req, res) {
    try {
    let response = await fetch("https://api.mapbox.com/geocoding/v5/mapbox.places/" + "Omaha" + ".json?access_token=pk.eyJ1IjoiYW5kcmV3Z3JheSIsImEiOiJjanZ5ZWlwMDkwZjNpNGRtcXJrajQ3cG44In0.Yn7UkfJamhhLMtEolCxfOw&autocomplete=true");
    let data = await response.json();
    let features = (data['features']);
    var long = features[0].center[0];
    var lat = features[0].center[1];
    var place = data.features[0].text;
// the place variable to what I need to send with the getweather function
    //console.log(lat, long);
    getWeather(lat, long, place);
    async function getWeather(lat, long, place) {
        try{
        //console.log(lat, long);
        let call = await fetch('https://api.darksky.net/forecast/7feabe44ce85c5bfa8afea21eadcea07/' + lat + ',' + long);
        let data = await call.json();
        //console.log(data);
        console.log(data);
        return res.send(data);
        } catch (err) {
            return res.status(500).send({error: err})
        }
    }
    } catch (err) {
        return res.status(500).send({error: err})
    }
}

我需要将place变量传递给getweather函数,并将其附加到数据中,然后发送数据。

我最终使用了

Data[location] = {location: place}
Res.send(data)

0 个答案:

没有答案