简体   繁体   English

Node.js 第 3 方 API 使用 Express 调用

[英]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.这就是为什么responsebody都是空的。 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:需要注意的几点:

  1. 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()是因为它看起来非常好用、简单、快速,并且完全基于承诺。

  2. 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.这将为您节省大量的调试时间。

  3. 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.

相关问题 Node.js 第 3 方 REST API 调用 - Node.js 3rd party REST API call 测试节点表达端点和存根第三方api调用 - testing node express endpoint and stub 3rd party api call 与运行长时间作业的第3方API同步的最佳做法(javascript,node.js) - Best practice to sync with 3rd party API running long jobs (javascript, node.js) 如何向第三方API写入Node.js请求? - How do I write a Node.js request to 3rd party API? 第三方API调用的node.js应用程序级速率限制 - node.js application-level rate limiting of 3rd party API calls 如何从Node(server.js)调用Gandi.Net之类的第三方API - How to call 3rd party api like Gandi.Net from Node (server.js) 使用没有第三方服务的Node JS发送短信 - Send SMS using Node JS without a 3rd party service Node / Express应用程序可查询第三方API并将响应存储在memcache / redis中30分钟以备将来查询 - Node/Express app to query a 3rd party API and store response in memcache/redis for 30 mins for future queries 如何从第三方node.js服务器验证Game Center用户 - How to authenticate Game Center User from 3rd party node.js server 如何将其他信息传递给node.js中的第三方模块的回调 - How to pass additional information to a callback of a 3rd party module in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM