ReferenceError: Invalid left-hand side in assignment when really assigning a value


ReferenceError: Invalid left-hand side in assignment when really assigning a value



Why does this snippet in node (10.5)


.then(function() {
this = {...this, ...payload};
this.update();
resolve({ok:true, node});
});



gives the following error:


ReferenceError: Invalid left-hand side in assignment



the payload holds a few properties that need to be added to this or if the property exists the property needs to be updated.


this



I do not understand why there is this error :(





You cannot assign this. Some more details : stackoverflow.com/questions/9713323/…
– Seblor
Jun 29 at 8:47



this




1 Answer
1



Well the Error is staightforward and says Invalid left-hand side in assignment, it means that you are using a wrong element in the left hand side of your assignment.


Invalid left-hand side in assignment



And that's because you are writing this = {...this, ...payload};, where you were trying to write a value to this in tour function, which is wrong and not possible, because you can't change this and assign a value to it as it's not permitted in JavaScript.


this = {...this, ...payload};


this


this



If you check the MDN this Reference you can see that:



In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called.


this





I see makes sense. Thank you
– Samuel
Jun 29 at 8:56





@Samuel You are welcome, glad it helps.
– chŝdk
Jun 29 at 8:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift