问题
I am doing programming in Office.js
. I'm trying to read the data which I get from the server.
Https.get
return the dada for sure! (I print it one second before sending it back from the server and also get a 200 code).
The issue is that I am not able to take the data I got from server and print it or o some calculations with it.
I looked for an answer everywhere but it just doesn't work for some reason I don't get.
var request = https.get("https://localhost:8888/getLastDetailedOrders", function(response){
//console.log(response.statusCode); //Testing the response.
//read the data
response.on('data', function (chunk){
body += chunk;
});
//ending the event.
response.on('end', function() {
range2.values = [[body]];
console.log(body); // prints nothing!
console.log('No more data in response.');
})
});
Please help me to read the data and treat it
回答1:
I inderstood that there was a race condition. The async function of officejs in which i wrote the https request simply ended its work before the server had the time to send back the information and the client to catch it. Even though there was a call back, since office needs the context paramater and it was not here any more because the function had been ended, the client could nt do any thing with the information being returned by the server. So the solution was to make a new async function where i would put the https request, this function return a promise. Then from the main function call the new function by adding await first before the call. Then .then() after the call to do all the rest tasks of the main function. This way you force the main function to wait until we get the full response from the server.
来源:https://stackoverflow.com/questions/56355880/how-to-get-the-body-of-https-response