0

So I have this code which essentially sends a message with results as a string then sends a message again to ask the user to select one of the elements inside the results array and then tries to assign the array content of results inside the myObj property using this which does not work (returns undefined).

I've read that this can refer to

the global object in a function.

can that apply here?.

Here is the sample:

                        let myObj = {
                           testArray: []
                        };

                        let results = [1,2,3]´;

                        send(results.join("\n"))//sends array as message
                            .then(m => { //premise returns the same array as message
                                send(`Please type a number.`)
                                    .then(m => { //same for reply, returns `Please type a number`
                                        obj = {
                                            testArray: this.results //<---- here results is undefined
                                        };
                                    })
                                    .catch(err => console.error(err));
                            })
                            .catch(err => {
                                console.error(err);
                            })
                    })

Would there be anyway to use the this keyword to get access to the results array defined earlier and if yes what would be it's role in this situation? Thanks in advance.

  • 1
    Save the reference in a `const` before the promise. `const _this = this` . Then in the promise just use `_this.results`. – Vladimir Bogomolov Feb 19 '20 at 12:55
  • The window object is the Global Object in the Browser. Any Global Variables or Functions can be accessed as properties of the window object. Because this changes depends on the context. – rootkonda Feb 19 '20 at 12:56
  • What is `obj`? Also a global variable? What role does `m` play? It seems the user's input is not influencing the results... – trincot Feb 19 '20 at 12:59
  • You used `results` already as the first `send` argument, so why do you want it differently the second time you use `results`? – trincot Feb 19 '20 at 13:01
  • Thank you for your answer @VladimirBogomolov, would you mind explaining why that would work and what does `_this` actually do there? thanks. –  Feb 19 '20 at 13:01
  • @SImplestDev I suggest reading https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – Vladimir Bogomolov Feb 19 '20 at 13:02
  • ...I see a lot more problems coming once you want to actually use `obj`. Maybe you should ask about the bigger picture, so we can better answer to what you really need here. – trincot Feb 19 '20 at 13:05

2 Answers2

0

Replace testArray: this.results with only testArray: results as results is a variable only

brk
  • 48,835
  • 10
  • 56
  • 78
0

The window object is the Global Object in the Browser. Any Global Variables or Functions can be accessed as properties of the window object. Because 'this' depends on the context.

Either assign 'this' to a variable and then reuse that variable inside the function or just say results(which may not be great option). Unless you have multiple 'results' variable defined you wont have a problem.

rootkonda
  • 1,700
  • 1
  • 6
  • 11