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.