[英]Node.js 3rd party API call using Express
Im using express with node.js & having trouble consuming data from this free api https://api.publicapis.org/entries?category=animals&https=true
any feedback will be helpful!我将 express 与 node.js 一起使用,并且无法使用此免费 api
https://api.publicapis.org/entries?category=animals&https=true
中的数据!
This code outputs the following error message TypeError: Cannot read property 'body' of undefined
此代码输出以下错误消息
TypeError: Cannot read property 'body' of undefined
request({
method: 'GET',
host: 'https://api.publicapis.org',
path: '/entries?category=animals&https=true',
}, function (error, response, body){
const data = response.body;
const apiData = JSON.parse(data)
console.log('Returned: ', apiData)
if(!error && response.statusCode == 200){
res.json(body);
}
else{
console.log("error with api call")
}
})
When I run your exact code, I get an error in the error
parameter.当我运行您的确切代码时,
error
参数中出现错误。 That's why both response
and body
are empty.这就是为什么
response
和body
都是空的。 You have an error.你有一个错误。 The specific error is this:
具体错误是这样的:
Error: options.uri is a required argument
at Request.init (D:\code\test\temp\node_modules\request\request.js:231:31)
at new Request (D:\code\test\temp\node_modules\request\request.js:127:8)
at request (D:\code\test\temp\node_modules\request\index.js:53:10)
at Object.<anonymous> (D:\code\test\temp\temp.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:956:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
Always check for errors before you try to use the other parameters and log any errors and it will save you a lot of time.在尝试使用其他参数并记录任何错误之前,请务必检查错误,这将为您节省大量时间。
If you change to this, it works for me:如果你改成这个,它对我有用:
const request = require('request');
request({
method: 'GET',
uri: 'https://api.publicapis.org/entries?category=animals&https=true',
}, function (error, response, body){
if (error) {
console.log(error);
return;
}
const data = response.body;
const apiData = JSON.parse(data)
console.log('Returned: ', apiData)
if(response.statusCode == 200){
console.log('success');
}
else{
console.log("error with api call")
}
});
Several things to note:需要注意的几点:
The request()
library has been deprecated. request()
库已被弃用。 While it will be maintained for a while (perhaps a long while), it will not be enhanced with new features any more.虽然它会被维护一段时间(也许很长一段时间),但它不会再通过新功能得到增强。 There's a list of alternatives that are still being actively developed here .
这里有一个仍在积极开发的替代方案列表。 I'm using
got()
because it seems very nice and simple and quick to use and it entirely promise-based.我使用
got()
是因为它看起来非常好用、简单、快速,并且完全基于承诺。
Always check for errors and log them before you try to use any of the other arguments.在尝试使用任何其他 arguments 之前,请务必检查错误并记录它们。 That will save you a ton of debugging time.
这将为您节省大量的调试时间。
When sending with res.json()
, you should pass it a Javascript object, not already converted JSON.使用
res.json()
发送时,您应该传递一个 Javascript object,尚未转换的 JSON。 Since you already have that with data
, that's what I changed your call to.既然你已经有了
data
,这就是我改变你的电话的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.