javascript - Typescript Array is undefined - Stack Overflow

I got a function which should save multiple JSON Objects into an Array of the Type "Contact"g

I got a function which should save multiple JSON Objects into an Array of the Type "Contact"

getContacts(){
    let self = this;
    $.ajax({
        type: "GET",
        url: "/chat/contacts/",
        dataType:"json",
        success: function(response){
            let obj = response;
            let i = 1;
            let contacts: Contact[] = [];
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    let val = obj[key];
                    contacts[i].id = val["id"]; //<-- contacts[i] is undefinded
                    contacts[i].partner = val["partnerId"];
                    contacts[i].name = val["name"];
                    contacts[i].type = val["type"];
                    console.log(contacts[i]);
                }
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(errorThrown);
        }
    });
}

At the marked point it says

contacts[i] is undefinded

How do i have to initialize the Array to make it working?

Here is the Contact Class:

class Contact extends BaseModel{
    static CCO_ID = "id";
    static CCO_PARTNER = "partner";
    static CCO_NAME = "name";
    static CCO_TYPE = "type";


    partner: Number;
    name: String;
    type: Number;
}

I got a function which should save multiple JSON Objects into an Array of the Type "Contact"

getContacts(){
    let self = this;
    $.ajax({
        type: "GET",
        url: "/chat/contacts/",
        dataType:"json",
        success: function(response){
            let obj = response;
            let i = 1;
            let contacts: Contact[] = [];
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    let val = obj[key];
                    contacts[i].id = val["id"]; //<-- contacts[i] is undefinded
                    contacts[i].partner = val["partnerId"];
                    contacts[i].name = val["name"];
                    contacts[i].type = val["type"];
                    console.log(contacts[i]);
                }
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(errorThrown);
        }
    });
}

At the marked point it says

contacts[i] is undefinded

How do i have to initialize the Array to make it working?

Here is the Contact Class:

class Contact extends BaseModel{
    static CCO_ID = "id";
    static CCO_PARTNER = "partner";
    static CCO_NAME = "name";
    static CCO_TYPE = "type";


    partner: Number;
    name: String;
    type: Number;
}
Share Improve this question asked Aug 31, 2017 at 6:55 TimTim 2553 gold badges6 silver badges18 bronze badges 1
  • use contacts.push – N1gthm4r3 Commented Aug 31, 2017 at 6:57
Add a ment  | 

1 Answer 1

Reset to default 3

You need first to define that contacts[i] is an object and then use it's properties.

And one more thing, you are starting from index 1, in Javascript array's index is starting from 0. Be aware if that is not intentionally.

let val = obj[key];
contacts[i] = new Contact(); // <-- Look here
contacts[i].id = val["id"];
contacts[i].partner = val["partnerId"];
contacts[i].name = val["name"];
contacts[i].type = val["type"];
console.log(contacts[i]);

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

相关推荐

  • javascript - Typescript Array is undefined - Stack Overflow

    I got a function which should save multiple JSON Objects into an Array of the Type "Contact"g

    19小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信