Juli15
Juli15

Reputation: 411

Create objects on Ionic

Good morning! I've been working with Ionic for a few weeks and I thought I actually understood how typescript and angular works, but I've found myself with a weird trouble that I think it will be very silly...

I'm trying to create an object called pendingWaybills with some properties named waybills, clients and clientWaybills. The thing is that I'm creating it this way:

  pendingWaybills: {
    waybills: any
    clients: any,
    clientWaybills: any,
  };

I've also tried

  pendingWaybills: {
    "waybills": any,
    "clients": any,
    "clientWaybills": any,
  };

And some other ways, but when I try to assign a value to this properties I'm getting the following error: TypeError: Cannot set property 'waybills' of undefined

I've also tried to assign some string or integers just to see if it was about the data that I was trying to assign like this.pendingWaybills.waybills = "Hi"; but I'm still getting the same error...

Would be glad to get some help as I think it's all about the way to create the object (and I also think it will be very silly) but I'm so stuck here. Thank you!

Edit: Here is where I try to assign the data to the object. (The variable data is a json)

  loadPendingWaybills(){
    this.loadClients(2)
      .then(data => {
        this.pendingWaybills.waybills = data;
        var preClients = this.pendingWaybills.waybills;
          this.clients = [];
            for(let i = 0;i < preClients.length; i++){
                if(this.pendingWaybills.clients.indexOf(preClients[i].descr1_sped) == -1){
                  this.pendingWaybills.clients.push(preClients[i].descr1_sped)
                }
            }
          this.pendingWaybills.clientWaybills = [];
            for(let i = 0; i < this.pendingWaybills.clients.length; i++){
              this.getWaybills(this.pendingWaybills.clients[i], 2)
                .then(data => {
                  if(this.pendingWaybills.clientWaybills[i] != data){
                    this.pendingWaybills.clientWaybills[i] = data;
                  }
                });
            }
      });
    }

Upvotes: 0

Views: 4663

Answers (3)

cjmling
cjmling

Reputation: 7278

Put this in constructor

constructor() {
   this.pendingWaybills = {
     waybills: [],
     clients: [],
     clientWaybills: [],
  };
}

Some explanation

It is nested object you can not create right away when you declare it.

For example var xxx = 'hi'; is fine. but if you do var xxx.yyy = 'hi' is not fine, as xxx is not defined before so yyy of xxx will cause error.

You can do

var xxx = {
   yyy: 'hi'
};

or you can do

var xxx = {};
xxx.yyy = 'hi';

Upvotes: 1

Augustin R
Augustin R

Reputation: 7799

In Typescript, doing :

pendingWaybills: {
    waybills: any;
    clients: any;
    clientWaybills: any;
};

will only set pendingWaybills variable type. To declare and assign value to the variable, you must do something like :

pendingWaybills = { // note the "="
    waybills: something,
    clients: something,
    clientWaybills: something,
};

Upvotes: 2

Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

You need to create an empty instance of the object, declaring the properties doesn't create the variable, it only tells your ide which properties it has:

public pendingWaybills = {
    waybills: []
    clients: [],
    clientWaybills: [],
};

Upvotes: 2

Related Questions