【问题标题】:Trying to post data with Request NPM from NodeJS to localhost (Loopback Swagger API)尝试使用 Request NPM 从 NodeJS 将数据发布到本地主机(Loopback Swagger API)
【发布时间】:2019-06-29 00:29:48
【问题描述】:

当我尝试使用此代码从 nodejs 中将实例发布到环回时,我没有收到任何错误,但也没有发布任何数据?

//NPM Package (request)
var request = require('request'); 

// Address of Loopback API on the same server
var api = "http://localhost:3000/api/devices"; 

//JSON Construction
var deviceInstance = {
     "manufacturer": "manufacturer",
     "model": "model"
   //etc
}

// NPM (request)
request({
   url: api,
   method: "POST",
   headers: {"Accept: application/json"},
   json: true,
   body: deviceInstance
}, function (error, response, body) {
      if(error) {
        console.log('error: '+ error);
      } else {
        console.log('document saved to api')
        console.log(body);
        console.log(response);
      }
});

process.exit();

我没有收到来自同一台机器的服务器的任何响应或错误。如果我在邮递员(Windows 应用程序)中尝试相同的调用,它实际上会在 API 中创建一个实例,那么为什么我的本地节点没有连接到 API?

【问题讨论】:

    标签: javascript node.js json mongodb loopback


    【解决方案1】:

    发生了什么以及为什么

    您看到的行为是由于 Javascript 的异步性质。

    您的代码从上到下启动 POST 请求,然后在请求完成之前调用process.exit(),这会给出您所看到的行为并“破坏”您的代码。

    从那里你有两个解决方案:

    在请求的回调中调用process.exit()

    //NPM Package (request)
    var request = require('request'); 
    
    // Address of Loopback API on the same server
    var api = "http://localhost:3000/api/devices"; 
    
    //JSON Construction
    var deviceInstance = {
         "manufacturer": "manufacturer",
         "model": "model"
       //etc
    }
    
    // NPM (request)
    request({
       url: api,
       method: "POST",
       headers: {"Accept: application/json"},
       json: true,
       body: deviceInstance
    }, function (error, response, body) {
          if(error) {
            console.log('error: '+ error);
          } else {
            console.log('document saved to api')
            console.log(body);
            console.log(response);
          }
          //request is done, we can safely exit
          process.exit();
    });
    

    request 的回调中调用 exit() 函数将有效地确保 POST 请求已完成并且您可以安全退出。

    完全删除 process.exit()

    事实上,您不需要手动退出:一旦 事件循环 为空,任何 Node 进程都会自行退出。换句话说,一旦没有为进程安排进一步的任务,节点就会自行退出进程。

    您可以在官方文档中找到更多信息:https://nodejs.org/api/process.html#process_event_exit

    【讨论】:

      【解决方案2】:

      为什么process.exit()

      调用 process.exit() 将强制进程尽快退出,即使仍有异步操作未决。

      【讨论】:

      • Source。您可以通过将process.exit() 移动到request 回调中来解决此问题
      • 这是一个将数千个项目插入环回的脚本,一旦完成,我希望节点停止。我无法将 process.exit 移到回调中,因为此代码处于循环中。
      • 但是我什至无法从 nodejs 发布这个测试项目
      • 这是奇怪的事情代码有点与邮递员一起工作但不是来自nodejs,邮递员我在环回中收到一个帖子,但我没有看到制造商或型号数据只是环回api中的一个新ID
      【解决方案3】:

      request 需要回调:

      request({
        url: api + "Devices",
        method: "POST",
        headers: "Accept: application/json",
        json: true,
        body: JSONParent
      }, (err, result, body) => {
        // do your stuffs with the results
      });
      

      【讨论】:

      • 同样的问题 没有来自服务器的响应 我已经更新了我的问题以显示我是如何进行错误报告的。目前,即使我指定 console.log 响应、正文和任何错误,请求之后也没有任何返回。
      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 2020-12-15
      相关资源
      最近更新 更多