2016-04-11 23 views
2

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 
... 

cevap

1

Aşağıda, projede kullanılan kurucu yer almaktadır. Veri kaynağınızı veri kaynağınıza koymayın.Aksi takdirde ListView, DataSource Object'in özelliklerini argüman olarak renderRow ile çağırır ve orijinal sorudaki tüm garip davranışlara yol açar. yerine verilerle dataSource ait

constructor(props) { 
    super(props); 
    let dataSource = new ListView.DataSource({ 
     rowHasChanged: (row1, row2) => row1 !== row2, 
    }); 

    this.state = { 
     dataSource: dataSource.cloneWithRows(dataSource), // Bad 
     items: realm.objects('Item').sorted('name'), 
    }; 
    this.componentDidMount = this.componentDidMount.bind(this); 
    } 

    componentDidMount() { 
    this.setState({ 
     dataSource: this.state.dataSource.cloneWithRows(this.state.items), 
    }); 
    } 

Seti durumu:

... 
    let data = realm.objects('Item').sorted('name') 
    this.state = { 
     dataSource: dataSource.cloneWithRows(data), 
     items: data, 
    }; 
... 

benim dataSource kurulumu oldukça standart olmadığı anlaşıldı!

0

için

adb logcat ReactNative:V ReactNativeJS:V 

derli toplu ile konsolda izlerseniz neden bilim için günlük hatları tuttu, benzediğini göstermektedir Bir Item, her zaman bir department'a sahip değildir.

Trying item.department returns a RealmObject, assumedly the one that is linked. item.department.name throws the error "undefined is not an object (evaluating 'item.department.name')"

Yani benim tahminim item.departmentitem.department.name erişmeyi denemeden önce ilk varsa kontrol edilmez bazı kodlar var. item.department tanımlanmamışsa, aldığınız hata iletisi mantıklı olacaktır. Bu yardımcı olursa veya bir şeyi yanlış anlamışsam lütfen bana bildirin!

+0

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. –

İlgili konular