我正在编写代码以在json数据中添加与每一行相对应的数据。 所有功能和代码编写均已完成。调用完所有数据后,应返回json。 但是在执行所有代码之前,它会返回json。 因此,即使所有代码运行正常,json始终返回一个空值。 请告知。
var request = require('request');
var axios = require('axios');
router.get('/geometric/getAddress', async (req, res) => {
let confmKey = "secret";
let resultType = "json";
let countPerPage = "10"
let page = appjs.vCheck(req.query.page, "");
let kw = appjs.vCheck(req.query.keyword, "");
var stmt = "http://10.10.100.12:8080/addr.do?";
stmt = stmt + "currentPage=" + page;
stmt = stmt + "&countPerPage=" + countPerPage;
stmt = stmt + "&resultType=" + resultType;
stmt = stmt + "&confmKey=" + confmKey;
stmt = stmt + "&keyword=" + qs.escape(kw)
request(stmt, async (err, result) => {
if (err) throw err;
const con = JSON.parse(result.body);
const juso = con.results.juso;
var dd = ""
let res_data = juso.map(async (addr) => {
let admCd = appjs.vCheck(addr.admCd, "");
let rnMgtSn = appjs.vCheck(addr.rnMgtSn, "");
let udrtYn = appjs.vCheck(addr.udrtYn, "");
let buldMnnm = appjs.vCheck(addr.buldMnnm, "");
let buldSlno = appjs.vCheck(addr.buldSlno, "");
axios.post("http://localhost:3001/web/api/geometric/coordinates",
{
admCd : admCd,
rnMgtSn : rnMgtSn,
udrtYn : udrtYn,
buldMnnm : buldMnnm,
buldSlno : buldSlno
}).then((d) => {
return {
"roadAddr" : d.roadAddr,
"jibunAddr" : roadAddr,
"zipNo" : d.roadAddr,
"longitude" : roadAddr,
"latitude" : d.roadAddr
}
})
})
let aa = await Promise.all(res_data)
res.json(aa)
})
});
答案 0 :(得分:0)
之所以发生这种情况,是因为您没有从map
回调中返回任何诺言。您应该执行以下操作:
let res_data = juso.map(async (addr) => {
let admCd = appjs.vCheck(addr.admCd, "");
let rnMgtSn = appjs.vCheck(addr.rnMgtSn, "");
let udrtYn = appjs.vCheck(addr.udrtYn, "");
let buldMnnm = appjs.vCheck(addr.buldMnnm, "");
let buldSlno = appjs.vCheck(addr.buldSlno, "");
return axios.post("http://localhost:3001/web/api/geometric/coordinates",
{
admCd : admCd,
rnMgtSn : rnMgtSn,
udrtYn : udrtYn,
buldMnnm : buldMnnm,
buldSlno : buldSlno
})
})
let aa = (await Promise.all(res_data)).map(({data: d}) => ({
"roadAddr" : d.roadAddr,
"jibunAddr" : roadAddr,
"zipNo" : d.roadAddr,
"longitude" : roadAddr,
"latitude" : d.roadAddr
}));
res.json(aa)
答案 1 :(得分:0)
由于您没有从promise
函数返回任何map
,因此await Promise.all(res_data)
是简单的代码。
更新:
let res_data = juso.map(async addr => {
let admCd = appjs.vCheck(addr.admCd, "");
let rnMgtSn = appjs.vCheck(addr.rnMgtSn, "");
let udrtYn = appjs.vCheck(addr.udrtYn, "");
let buldMnnm = appjs.vCheck(addr.buldMnnm, "");
let buldSlno = appjs.vCheck(addr.buldSlno, "");
return axios.post("http://localhost:3001/web/api/geometric/coordinates", {
admCd: admCd,
rnMgtSn: rnMgtSn,
udrtYn: udrtYn,
buldMnnm: buldMnnm,
buldSlno: buldSlno
});
});
然后为返回的那些承诺await
(对于那些失败的承诺中的任何一个try/catch
或Promise.all(res_data).then().catch(e)
也可以处理错误):
await Promise.all(res_data)
.then(resolvedPromises => {
const aa = resolvedPromises.map(d => ({
roadAddr: d.roadAddr,
jibunAddr: d.roadAddr,
zipNo: d.roadAddr,
longitude: d.roadAddr,
latitude: d.roadAddr
}));
return res.json(aa);
})
.catch(e => {
//handle error because one of those axios promises failed: e.message
});