代码之家  ›  专栏  ›  技术社区  ›  user1hjgjhgjhggjhg

如何在节点js中执行多个Post请求

  •  1
  • user1hjgjhgjhggjhg  · 技术社区  · 7 年前

    你好,我想根据一些条件做多个post请求。我正在尝试,但我的代码不起作用。我有一个在线数据库(Firebase)。我要做的是从联机获取数据并保存到localdb中,然后删除联机数据。

    这就是我目前所做的

    request('http://my-url here', function (error, response, body) {
        console.log('error:', error);
        console.log('statusCode:', response && response.statusCode); Print the response status code if a response was received
    
        var data = [];
        var parse = JSON.parse(body);
    
        var ids =  Object.keys(parse);
         var i = 0;
    
       ids.forEach(function(id) {
       var unique_keys =  Object.keys(parse[id]);
            unique_keys.forEach(function(unique_key) {
    
                data[i] = [
    
                        parse[id][unique_key]["lat"],
                        parse[id][unique_key]["long"],
    
    
    
                    ];
                    i++;
       });
    
     });
    
        saveHistory(data, function (result) {
    
            console.log("successfully save into local db");
            removeDataFromOnlineDatabase
            process.exit();
    
        });
    
    
    
    
    
    }); 
    
    function removeHistoryFromOnlineDatabase(){
    
        var request = require("request");
    
       console.log("function get called");
        var options = { method: 'DELETE',
            url: 'url here',
            headers:
                { 'Postman-Token': '4a126ab8-9b3e-465d-b827-d4dd83923610',
                    'Cache-Control': 'no-cache',
                    'Content-Type': 'application/json' } };
    
        request(options, function (error, response, body) {
            if (error) throw new Error(error);
    
            console.log("history has been removed" + body);
        });
    
    }
    

    我试过上面的代码,但是这个函数 removeHistoryFromOnlineDatabase 后请求不起作用

    函数get called并打印“function get called”,但它不打印“history has been remove”

    2 回复  |  直到 7 年前
        1
  •  0
  •   bharath    7 年前

    移动 process.exit(); 在下面 console.log("history has been removed" + body);

        2
  •  0
  •   Hardik Shah    7 年前

    因为您正在调用函数,然后立即行退出进程。 process.exit() 在你的 removeHistoryFromOnlineDatabase 函数执行。

    请注意:- 我已经删除了较小代码的JSON数据选项。


    方式1: (使用 Callback ) (仅回拨)

    saveHistory(data, function (result) {
    
        console.log("successfully save into local db");
        removeDataFromOnlineDatabase(function(error, response){
           process.exit();
        })
    
    });
    
    function removeHistoryFromOnlineDatabase(callback){
      var request = require("request");
      console.log("function get called");
      var options = options;
    
      request(options, function (error, response, body) {
        if (error){
         callback(error, null); 
        } else {
         console.log("history has been removed" + body);
         callback(null, response) 
        }
      });
    }
    

    方式2: (使用 Promise ) (仅承诺)

    var Q = require("q");
    
    saveHistory(data, function (result) {
        console.log("successfully save into local db");
        removeDataFromOnlineDatabase()
        .then(function(){
          process.exit();
        }).catch(function(){
           console.log("ERROR IN REQUEST");
        });
    });
    
    function removeHistoryFromOnlineDatabase(){
      var request = require("request");
      console.log("function get called");
      var options = options;
    
      return Q.promise(function(resolve, reject){
        request(options, function (error, response, body) {
          if (error){
            reject(error);
          } else {
            console.log("history has been removed" + body);
            resolve(response) 
          }
        });
      });
    
    }
    

    方式3: (使用 promise - Q.nfcall ) (回拨+承诺)

    var Q = require("q");
    
    saveHistory(data, function (result) {
        console.log("successfully save into local db");
        Q.nfcall(removeDataFromOnlineDatabase)
        .then(function(){
          process.exit();
        }).catch(function(){
           console.log("ERROR IN REQUEST");
        });
    });
    
    function removeHistoryFromOnlineDatabase(callback){
      var request = require("request");
      console.log("function get called");
      var options = options;
    
      request(options, function (error, response, body) {
        if (error){
         callback(error, null); 
        } else {
         console.log("history has been removed" + body);
         callback(null, response) 
        }
      });
    
    }
    
    推荐文章