最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript Array is undefined - Stack Overflow

programmeradmin0浏览0评论

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]);
发布评论

评论列表(0)

  1. 暂无评论