javascript - Angular 2 | cannot assign to array because it is a constant or a read-only property - Stack Overflow

I'm trying to make a get request to my backend api that returns a array of objects. I'm using

I'm trying to make a get request to my backend api that returns a array of objects. I'm using following code:

smallponent.ts (when calling openDepositModal() it calls the function getUserInventory() from auth.service.ts which makes a get request to my backend api that then returns an array with objects.)

[...]

export class Item{
    id: String;
    name: String;
    img: String;
    value: String;
}

const items: Item[] = [];

[...]

openDepositModal(){
    if(!items){
        this.authService.getUserInventory().subscribe(data => {
            items = data; <-- ERROR HERE
        });
    }
}

auth.service.ts

[...]

getUserInventory(){
    let headers = new Headers();
    this.loadToken();
    headers.append('Authorization', 'JWT ' + this.authToken);
    headers.append('Content-Type', 'application/json');
    return this.http.get('http://localhost:3000/api/user/inventory', { headers: headers })
    .map(res => res.json());
}

[...]

Inside of smallponent.ts I am trying to insert the data I got from the service into the "items" array. But I get the error "cannot assign to array because it is a constant or a read-only property". Can someone fix my code? Thanks. :-)

I'm trying to make a get request to my backend api that returns a array of objects. I'm using following code:

small.ponent.ts (when calling openDepositModal() it calls the function getUserInventory() from auth.service.ts which makes a get request to my backend api that then returns an array with objects.)

[...]

export class Item{
    id: String;
    name: String;
    img: String;
    value: String;
}

const items: Item[] = [];

[...]

openDepositModal(){
    if(!items){
        this.authService.getUserInventory().subscribe(data => {
            items = data; <-- ERROR HERE
        });
    }
}

auth.service.ts

[...]

getUserInventory(){
    let headers = new Headers();
    this.loadToken();
    headers.append('Authorization', 'JWT ' + this.authToken);
    headers.append('Content-Type', 'application/json');
    return this.http.get('http://localhost:3000/api/user/inventory', { headers: headers })
    .map(res => res.json());
}

[...]

Inside of small.ponent.ts I am trying to insert the data I got from the service into the "items" array. But I get the error "cannot assign to array because it is a constant or a read-only property". Can someone fix my code? Thanks. :-)

Share Improve this question asked Sep 22, 2017 at 19:17 User3434343443User3434343443 1703 silver badges17 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

use let instead of const

e.g. let items: Item[] = [];

const declarations are like let declarations but, as their name implies, their value cannot be changed once they are bound. In other words, they have the same scoping rules as let, but you can’t re-assign to them.

let vs. const (docs)

Given that we have two types of declarations with similar scoping semantics, it’s natural to find ourselves asking which one to use. Like most broad questions, the answer is: it depends.

Applying the principle of least privilege, all declarations other than those you plan to modify should use const. The rationale is that if a variable didn’t need to get written to, others working on the same codebase shouldn’t automatically be able to write to the object, and will need to consider whether they really need to reassign to the variable. Using const also makes code more predictable when reasoning about flow of data.

On the other hand, let is not any longer to write out than var, and many users will prefer its brevity. The majority of this handbook uses let declarations in that interest.

Replace error by this.items = data;

because you have declared it as an constant. declare it as a property in your class and then you can assign values to it

export class SmallComponent{
private items: Item[] = [];

[...]

openDepositModal(){
    if(!items){
        this.authService.getUserInventory().subscribe(data => {
            this.items = data; <-- HERE
        });
    }
}

If they are outside of ponent class then change const to let

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745451558a4628294.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信