如何进行多个API调用,node.js承诺

时间:2018-10-23 16:58:09

标签: javascript node.js

我是使用JavaScript进行承诺和异步编程的新手,而且我正尝试对Google Maps进行多个API调用,将API放置在node.js中,然后在收到所有数据后将数据发送到客户端。但是,我在参数列表后收到某种语法错误,说缺少)。抱歉,如果我要问一个愚蠢的问题,我似乎无法解决这个问题。谢谢您的帮助!

const fetch = require('node-fetch');

module.exports = (app) => {

    app.post('/search-champ', (req, res) => {


        console.log(req);
        let lat = req.body.param.lat; //before: req.query.lat it's wrong

        let long = req.body.param.long;
        console.log(lat);

        const apiId = 'AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M';
        const urls = [

            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=department_store&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M',

            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=food&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M'
        ];


        Promise.all(urls.map(url =>


            fetch(url)
            .then(checkStatus)


            .then(data => {

                console.log(data)
                res.send({
                    data

                });
            })

        }).catch(err => {
            res.redirect('/error');
        });

        function checkStatus(response) {
            if (response.ok) {
                return Promise.resolve(response);
            } else {
                return Promise.reject(new Error(response.statusText));
            }
        }




    })
}

3 个答案:

答案 0 :(得分:1)

将为数组中的每个URL调用res.send({ data });,这可能不是您想要的,等待所有api调用完成,将res.send()放在then()Promise.all()

Promise.all(urls.map(url =>
  fetch(url)
  .then(checkStatus)
).then(data => {
  console.log(data)
  res.send({ data });
}).catch(...)

答案 1 :(得分:0)

在javascript中获取返回一个响应承诺对象,您必须在其上调用json方法。将您的checkStatus方法更改为此。

function checkStatus(response) {
            if (response.ok) {
                return Promise.resolve(response.json());
            } else {
                return Promise.reject(new Error(response.statusText));
            }
        }

答案 2 :(得分:0)

工作解决方案:

const fetch = require('node-fetch');
module.exports = (app) => {
    app.post('/search-champ', (req, res) => {
        let lat = req.body.param.lat; //before: req.query.lat it's wrong
        let long = req.body.param.long;
        const apiId = 'AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M';
        const urls = [
            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=department_store&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M',
            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=food&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M'
        ];
        var apiData = urls.map( (url) => {
            return fetch(url).then(checkStatus);
        });
        Promise.all(apiData)
            .then( (data) => {
            res.send({
                data: data
            });
        });

        function checkStatus(response) {
            if (response.ok) {
                return Promise.resolve(response);
            } else {
                return Promise.reject(new Error(response.statusText));
            }
        }
    });
}