在第一个函数 - REST之后执行第二个函数

时间:2017-02-08 15:45:52

标签: javascript node.js rest

如果第一个REST函数使用sucess执行,第二个将使用第一个函数的参数执行,在返回:sessionid的情况下,我将值保存在变量sessionid中

这两个函数都在同一个REST文件中.js调用。

在我尝试的情况下:

我的 restApiCall.js 档案

var Client = require('./lib/node-rest-client').Client;
var client = new Client();

var dataLogin = {
   data: { "userName":"xxxxx","password":"xxxxxxxxxx","platform":"xxxx" },
   headers: { "Content-Type": "application/json" }
};

var numberOrigin = 350;

client.registerMethod("postMethod", "xxxxxxxxxxxxxxxxxx/services/login", "POST");

client.methods.postMethod(dataLogin, function (data, response) {
   // parsed response body as js object
   // console.log(data);
   // raw response
    if(Buffer.isBuffer(data)){
      data = data.toString('utf8');
      console.log(data);
      re = /(sessionID: )([^,}]*)/g;
      match = re.exec(data);
      var sessionid = match[2]
      console.log(sessionid);
      openRequest(sessionid, numberOrigin);  // I try execute, but just the first execute if I type inside Prompt command: node restApiCall
    }
});
// this is the second function I want execute after the first sucess 
function openRequest(sessionid, numberOrigin){
  var client = new Client();
  numberOrigin+=1;
   var dataRequest = {
   data: {"sessionID":sessionid,"synchronize":false,"sourceRequest":{"numberOrigin":numberOrigin,"type":"R","description":"Test - DHC","userID":"xxxxxxxxxx","contact":{"name":"Sayuri Mizuguchi","phoneNumber":"xxxxxxxxxx","email":"xxxxxxxxxxxxxxxxxx","department":"IT Bimodal"},"contractID":"1","service":{"code":"504","name":"Deve","category":{"name":"Developers"}}} },
   headers: { "Content-Type": "application/json" }
   };
   client.post("xxxxxxxxxxxxxxxxxxxxxxxxx/services/request/create", dataRequest, function (data, response) {
   // parsed response body as js object
   // console.log(data);
   // raw response
   console.log(response);
   });
}

谢谢你。

1 个答案:

答案 0 :(得分:0)

如果问题是new Client();,因为客户已经使用第2行进行了定义,并且不需要再次声明。

我使用此代码并且工作正常。

var Client = require('./lib/node-rest-client').Client;
var client = new Client();  //defined


var dataLogin = {
   data: { "userName":"xxxxx","password":"xxxxxxxxxx","platform":"xxxx" },
   headers: { "Content-Type": "application/json" }
};

var numberOrigin = 350;

client.registerMethod("postMethod", "xxxxxxxxxxxxxxxxxx/services/login", "POST");

client.methods.postMethod(dataLogin, function (data, response) {
   // parsed response body as js object
   // console.log(data);
   // raw response
    if(Buffer.isBuffer(data)){
      data = data.toString('utf8');
      console.log(data);
      re = /(sessionID: )([^,}]*)/g;
      match = re.exec(data);
      var sessionid = match[2]
      console.log(sessionid);
      openRequest(sessionid, numberOrigin);  // execute fine
    }
});

function openRequest(sessionid, numberOrigin){
  numberOrigin+=1;
   var dataRequest = {
   data: {"sessionID":sessionid,"synchronize":false,"sourceRequest":{"numberOrigin":numberOrigin,"type":"R","description":"Test - DHC","userID":"xxxxxxxxxx","contact":{"name":"Sayuri Mizuguchi","phoneNumber":"xxxxxxxxxx","email":"xxxxxxxxxxxxxxxxxx","department":"IT Bimodal"},"contractID":"1","service":{"code":"504","name":"Deve","category":{"name":"Developers"}}} },
   headers: { "Content-Type": "application/json" }
   };
   client.post("xxxxxxxxxxxxxxxxxxxxxxxxx/services/request/create", dataRequest, function (data, response) {
   // parsed response body as js object
   // console.log(data);
   // raw response
   console.log(data);
   });
}