循环遍历nodejs中插入的方法

时间:2017-02-07 11:25:03

标签: node.js asynccallback

我需要插入三件事

  1. 国家
  2. 状态
  3. 城市
  4. async.waterfall方法有insertCountry

    insertcountry包含以下逻辑:

    var listofCountry = "source file to read the list of country" //a file is there from where i read the countries
    listofCountry.forEach(function(objCountry) {
      createCountry(data, next) //with some logic i have my data object already filled.
      createState() //in this method i need to pass the countryId that i have just created.
    }, this);
    
    //this is the method that will carry insert of country, **Request data** is a generic method and works fine.
    
    function createCountry(data, next) {
    
      Request(data, {
          api: "myApi",
    
          uri: "/Country/create",
          method: "POST",
          data: {
            "name": "data.CountryName",
          }
        },
        function(error, response) {
          if (response && response.data !== undefined && response.data !== null) {
            data.countryId = response.data.CountryId; // the debugger is not coming here.hence not setting the countryId
          } else if (response && response.error !== undefined && response.error !== null) {
            error = response.error;
          }
        });
    }
    

    我觉得asynccallback的实施方式不正确,我不确定。

0 个答案:

没有答案