Bağlantılı bir nesnenin özelliklerine ana nesne üzerinden erişmeye çalışıyorum, ancak yalnızca tanımsız olarak geri dönüyorum. Bu, ListView'ın renderRow yöntemi/prop içindeki koddur (Realm ListView kullanıyorum). Android'de iOS'u test etmediniz. Bağlanan nesnenin kendisi iyi çalışıyor gibi görünüyor. İşte bazı (kesilmiş) kodu bağlam:Realm React-Native ListView öğesindeki bağlantılı nesnelerin özellikleri döndürülemez (hata: undefined bir nesne değil)
// Schemas being used to create realm (trimmed down to relevant)
class Item {};
Item.schema = {
name: 'Item',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string',
department: 'ItemDepartment',
}
}
class ItemDepartment {};
ItemDepartment.schema = {
name: 'ItemDepartment',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string',
parentDepartment: 'ItemDepartment'
}
}
// renderRow method (trimmed but same error thrown)
renderRow(item) {
return (
<Text>{item.department.name}</Text>
);
}
alemine poking her iki nesneleri düzgün tüm özelliklere sahip örneği görünür bana gösteriyor.
Her Öğe dizesi ve sayı özelliklerine ince (ör. Item.name) erişebilirim.
Öğe deneniyor öğe, bağlı olan varsayılan bir RealmObject değerini döndürür.
item.department.name
Bu renderRow ince dışında çalıştığını "tanımsız ('item.department.name' değerlendirirken) bir nesne değil" hatası atar.
renderRow(item) {
console.log('item.name: ' + item.name)
console.log('item.department: ' + item.department)
return (
<Text>{item.name}</Text>
);
}
sonucun:
ReactNativeJS: Running application “undisclosed” with appParams: {"initialProps":{},"rootTag":1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
ReactNativeJS: item.name:
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: defaultGetRowData
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name:
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: defaultGetSectionHeaderData
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: undefined
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: undefined
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: undefined
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: undefined
ReactNativeJS: item.department: undefined
ReactNativeJS: item.name: MockItem1
ReactNativeJS: item.department: [object RealmObject]
ReactNativeJS: item.name: MockItem2
ReactNativeJS: item.department: [object RealmObject]
ReactNativeJS: item.name: MockItem3
ReactNativeJS: item.department: [object RealmObject]
İlginçtir ki ListView çalışmaları
çevresinde alay uzun yol sorunu renderRow içinden bu console.log içinde yatıyor düşünmek bana yol vardır renderRow hiçbir şey ve undefined ile defalarca denir rağmen.
Red Screen of death - undefined is not an object
Herhangi geribildirim ve öneriler değerlendirilecektir: En kısa sürede olsa item.department.name girişimi olarak! Bu, Realm RN ListView ile bir hata mıdır, yoksa bu amaçlanan şekilde çalışıyor mu, fakat benim uygulama yanlış mı?
GÜNCELLEME
: şimdilik etrafında kirli işlerini var: öğenin kod özelliği truthy olup olmadığınıgetItemDepartmentName(item) {
let code = item.code;
console.log("item: " + item)
console.log("code: " + code)
console.log("typeofX: " + typeof code)
if (code) {
console.log("code True: true")
return item.department.name
}
console.log("code True: false");
return "Undefined"
}
renderRow(item) {
return(
<Text>{this.getItemDepartmentName(item)}</Text>
)
}
// for this to work you must bind this on renderRow in listView:
<ListView
...
renderRow={this.renderRow.bind(this)}
.../>
Temelde sadece kontrol. İlk renderRows'u atlar. Bu düzeltme işleri/böcek sorunuzun ve günlükleri bakarak okunabilirliği
ReactNativeJS: item: function (row1, row2) {return row1!==row2;}
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item: function defaultGetRowData(dataBlob, sectionID, rowID) {
ReactNativeJS: return dataBlob[sectionID][rowID];}
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item: function() {return false;}
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item: function defaultGetSectionHeaderData(dataBlob, sectionID) {
ReactNativeJS: return dataBlob[sectionID];}
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item:
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item:
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item:
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item:
ReactNativeJS: code: undefined
ReactNativeJS: typeofX: undefined
ReactNativeJS: code True: false
ReactNativeJS: item: [object RealmObject]
ReactNativeJS: code: MI1
ReactNativeJS: typeofX: string
ReactNativeJS: code True: true
ReactNativeJS: item: [object RealmObject]
ReactNativeJS: code: MI2
ReactNativeJS: typeofX: string
ReactNativeJS: code True: true
ReactNativeJS: item: [object RealmObject]
ReactNativeJS: code: MI3
ReactNativeJS: typeofX: string
ReactNativeJS: code True: true
...
Düzeltmemle birlikte iyi çalışıyor ve tüm öğelerin bu aşamada bölüm oluşturduğundan eminim. Sorun, ListView'den renderRow ile geliyor, gerçekten de yapmamam gerektiğini düşünüyorum - veri kaynağının işlevleri ve günlükte görebildiğiniz gibi hiçbir girişe sahip olmayan bir demet. Bunun ötesinde, 'item.department.name', gerçek bölge verileri üzerinde tamamen iyi çalışır. –