TypeError: Cannot set Property which has only a Getter in JS

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# TypeError: Cannot set Property which has only a Getter in JS

The "Cannot set property which has only a getter" error occurs when trying to set a new value to a property, for which only a getter is defined.

To solve the error, define a setter for the specific property.

index.js
class Person { constructor(first, last) { this.first = first; this.last = last; } // ๐Ÿ‘‡๏ธ getter get name() { return `${this.first} ${this.last}`; } } const p1 = new Person('John', 'Smith'); // โœ… Use getter console.log(p1.name); // โ›”๏ธ TypeError: Cannot set property name of #<Person> which has only a getter p1.name = 'John Doe';
The code for this article is available on GitHub

We created a class in which we defined a name getter.

The getter allows us to get the full name by accessing the name property on an instance of the class.

Any time we access the name property on an instance of the class, the name() getter method is run.

We then attempt to update the value of the name property of the instance.

However, we haven't defined a setter for the property.

# Define a setter for the property you are trying to update

To solve the error, define a setter for the property you are trying to update.

The set syntax allows us to specify a function to be run when the specific property is set.

index.js
class Person { constructor(first, last) { this.first = first; this.last = last; } get name() { return `${this.first} ${this.last}`; } // ๐Ÿ‘‡๏ธ Define setter set name(name) { this.first = name.split(' ')[0]; this.last = name.split(' ')[1]; } } const p1 = new Person('John', 'Smith'); // โœ… Use setter p1.name = 'Bobby Hadz'; console.log(p1.name); // ๐Ÿ‘‰๏ธ "Bobby Hadz"

define setter for property you are trying to update

The code for this article is available on GitHub

We defined a setter for the name property. This means that any time we try to set the property on an instance, the setter method is run.

We updated the values of the first and last class properties in the method.

Notice that the value to the right of the equal sign gets passed as a parameter to the setter method.

# Make sure you aren't trying to set a property by mistake

Here is another example of how the error occurs.

index.js
class Person { constructor(name) { // ๐Ÿ‘‡๏ธ this causes the error this.name = name; } // ๐Ÿ‘‡๏ธ getter get name() { return `Name: ${this.name}`; } } // โ›”๏ธ TypeError: Cannot set property name of #<Person> which has only a getter const p1 = new Person('bobby hadz');

make sure you are not trying to set property by mistake

The code for this article is available on GitHub

The code in the constructor() causes the error.

We defined a name() getter so when we run the this.name = name line in the constructor, we are trying to use a setter for the name property.

You either have to rename your getter or you have to rename the property in the constructor.

Here is an example of renaming the getter function.

index.js
class Person { constructor(name) { this.name = name; } // ๐Ÿ‘‡๏ธ getter get get_name() { return `Name: ${this.name}`; } } const p1 = new Person('bobby hadz'); console.log(p1.get_name); // ๐Ÿ‘‰๏ธ Name: bobby hadz

renaming the getter function

The code for this article is available on GitHub

We renamed the getter to get_name so now the name instance variable doesn't clash with the getter function.

When we access the get_name property on an instance of the class, the get_name function is invoked.

You might also be interested in reading about private fields in JavaScript classes.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev