2017-02-08 4 views
1

Wenn die erste REST Funktion mit Erfolg, die zweiten ausführt es mit den Parametern der ersten Funktion im Fall Rückkehr auszuführen sein wird: sessionid und ich den Wert in Variable sessionid speichernExecute zweite Funktion nach dem ersten Funktion - REST

Beide Funktionen sind REST Aufruf innerhalb der gleichen .js Datei. Ich

Im Fall versuchen:

MeinerestApiCall.jsDatei:

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); 
    }); 
} 

Dank voraus.

Antwort

0

In dem Fall ist das Problem new Client();, weil der Klient mit Linie 2 definiert worden ist und nicht notwendig, erklären Sie wieder.

Ich benutze diesen Code und funktioniert gut.

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); 
    }); 
} 
Verwandte Themen