【发布时间】:2018-10-12 11:26:35
【问题描述】:
我正在使用来自https://swapi.co/ 的数据在 node.js 中编写一个应用程序。我的一项功能需要检查我的数据库中是否存在指定的行星,如果不存在,api 应该从 swapi 下载行星的数据,但不知何故我无法检索有关按名称指定的行星的数据,我只能获取格式提供的数据此链接:https://swapi.co/api/planets/?format=json
- 当我尝试过滤或将此结果转换为 JSON 或过滤它时,nodejs 给了我一个错误,但在控制台中记录响应的正文显示它看起来很像 JSON,所以,问题是我如何才能拉出指定行星反应的身体?
方法代码:
router.route('/Planets')
.post(function (req, res) {
var planet = new Planet(req.body);
//validate planet.name here
Planet.find({name: planet.name}, function (err, planet) {
if (err) {
res.status(500).send(err);
}
if (planet == '') {
console.log("action: planet not found");
request.get(
'https://swapi.co/api/planets/?format=json',
{json: true},
function (error, response, body) {
console.log(body);
}
);
// planet.save();
res.status(201).send(planet);
} else {
res.status(201).send(planet);
}
})
})
.get(function (req, res) {
Planet.find(function (err, planets) {
if (err) {
res.status(500).send(err);
} else {
res.json(planets);
}
});
});
【问题讨论】:
-
它显示数据所以有什么问题?
标签: javascript json node.js filtering response