-1

I am working on JS right now and compare it to VB.Net.

I want to add a value to a string that I have declared before.

In VB.Net it would look like this:

Dim MyString as String
Mystring = "Test"

In JS it should work like this:

let Mystring;
Mystring = 'test';

However this doesn't work for me in the following Code:

var https = require('https');
let MyString = 'null';       //String that I want to declare

var request = https.request(options, function (res) {    //I do a Get Request to get a String from an URL
    var data = '';
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('end', function () {
        MyString = data; 
        console.log(MyString);     //Here I get the URL Content as Result for MyString
    });
});

console.log(MyString);  // Here I get 'null' as Result

I want the URL content data to be assigned to MyString so that I am able to read the Data outside of the function. How is that possible?

  • 3
    `const` declares a constant, use `let` instead. But you're still out of luck with this, see https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – Teemu Apr 10 '21 at 10:36
  • 1
    You need to change it to: `let MyString;` and `MyString = data;`. That `console.log(MyString)` outside still won't work. You might want to look at: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321) and [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086) – adiga Apr 10 '21 at 10:40

1 Answers1

2

There are three things going on there:

  1. const declares a "variable" you can't change the value of (a constant).

  2. By using let in the inner function, you're shadowing that constant with a local variable. There is no connection whatsoever between the inner local variable and the outer constant.

  3. At the end of your code, where you're logging the value, it will still have its initial value, because it hasn't been changed yet. The event callbacks won't have been called yet. Details on that in the answers to this question and this one.

If you want to add to the outer myString, use let to declare it, and don't use let when referencing it inside the function.

// SEE BELOW AS WELL
const https = require("https");
let myString = ""; // You probably want `""` or `null`, not `"null"`

var request = https.request(options, function (res) {
    let data = "";
    res.on("data", function (chunk) {
        data += chunk;
    });
    res.on("end", function () {
        // *** No `let` here
        myString = data; 
        console.log(myString);
    });
});

console.log(myString);  // You'll still get `""` or `null` here, it isn't updated yet

Notice that I replaced var with let or const, and changed the name of the variable from MyString to myString. There's no place for var in modern JavaScript code, and the overwhelmingly-common naming convention for variables is to start them with a lower case letter (initial caps are used for constructor function names).

But, because of #3 above, you really don't want myString at all. Instead:

const https = require("https");

var request = https.request(options, function (res) {
    let myString = "";
    res.on("data", function (chunk) {
        myString += chunk;
    });
    res.on("end", function () {
        // *** Just use `myString` here, don't try to use it
        // at the top level of your module at all.
        console.log(myString);
    });
});

Side note: Your code assumes that chunk will be a string, but I'm not sure that's true with the stream you get from https.request, but I'm not sure that's true. If so, great, but it may well be a Buffer object in which case += won't work (you'd need concat or similar).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875