运行reduce,map功能

时间:2020-04-19 09:33:53

标签: javascript dictionary

在country.json中我有3个类和数据

country.json

[{"country":"Algeria","region":"Northern Africa","code":"DZ","population":41318142,"cities":["Boumerdas","Zeribet el Oued","Zeralda"]},{"country":"American Samoa","region":"Polynesia","code":"AS","population":55641,"cities":["Aūa","Vaitogi","Vailoatai"]}

在countryRep类中,我执行此功能以获得人口总数

   async  getSumAllPopulation(){
            let countries = await this.getCountries();
           let populations= countries.map(s=>s.population).reduce((acc, s) => s + acc);
           return populations;
        }

在测试中我写

app.get('/PopulationSum', async (req, res) => {
    const countries = await repo.getSumAllPopulation();
    res.send(countries);
})

getCountries()函数

    async getCountries() {
        try {
            const countries = await fs.readJSON(this.countryFilePath);
            return countries;
        }
        catch (e) {
            console.log(e)
        }

    }

但是我没有得到总和的结果?我该如何解决?我收到一个错误消息,似乎该方法未返回任何内容。

enter image description here

0 个答案:

没有答案
相关问题