数组从 api 调用到 html 索引页面中的渲染/sho"/>
Node js 如何使用 json 解析数据数组从 api 调用到 html 索引页面中的渲染/sho
我所有的代码都运行正常,但我不知道如何使用这个 dataj (DATAJ) 数组在视图 index.html 的 html 元素中显示,我需要一些指导,经过多次尝试我做不到....
index: 函数 (req, res) {
const https = require('https');
CityModel.find().sort({name: 1}).exec(function (error, cities) {
if (error) {
return res.render('web/index', { url,dataj: [], cities: [], moment })
}
PostModel.find({ status: 'Publicado', main: true }).sort({name: 1}).exec(function (err, posts) {
if (err) {
return res.render('web/index', { url, cities, posts: [], moment });
}
从这里我需要使用响应信息
var options = {
host: 'host',
path: 'ruta',
headers: {'api-key': 'apitext'}
};
https.get(options, function (res) {
var json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', function () {
if (res.statusCode === 200) {
try {
var dataj = JSON.parse(json)
.map(ciudad => {
console.log( {ciudades:ciudad.name})
console.log(ciudad.name)
console.log(ciudad.tag)
});
console.log(json)
console.dir(dataj.body);
;
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
return res.render('web/index', { url, cities, posts, moment });
})
})
},
回答如下:
更多推荐
Node js 如何使用 json 解析数据数组从 api 调用到 html 索引页面中的渲染/sho
发布评论