推送到领域列表

问题描述:

这些是我的境界对象。我有洞和圆。我试图在一次写入中填充18个孔对象,但在过去的几个小时里我一直困在这里,我似乎无法理解我要出错的地方。推送到领域列表

class Hole extends Realm.Object {} 
Hole.schema = { 
    name: 'Hole', 
    primaryKey: 'id', 
    properties: { 
    id: 'int', 
    fullStroke: 'int', 
    halfStroke: 'int', 
    puts: 'int', 
    firstPutDistance: 'int', 
    penalties: 'int', 
    fairway: 'string' 
    }, 
}; 

class Round extends Realm.Object {} 
Round.schema = { 
    name: 'Round', 
    primaryKey: 'id', 
    properties: { 
    id: 'string', 
    done: 'string', 
    holes: {type: 'list', objectType: 'Hole'} 
    }, 
}; 

这是我的函数,试图将每个孔推入Round的孔属性。任何帮助将不胜感激。

exportRound =() => { 
    let holesObjects = realm.objects('Hole') 
    if(holesObjects.length < 9){ 
    alert('Enter stats for at least 9 holes please') 
    } 
    else{ 
    var sortedHoles = holesObjects.sorted('id') 
    currentRound = realm.objects('Round').filtered('done == "no"') 
    for(var i = 1; i < holesObjects.length; i++){ 
     console.log(holesObjects.filtered('id == i')) 
     realm.write(()=> currentRound.holes.push(holesObjects.filtered('id == {i}'))) 
    } 
    } 
} 

你面临的错误是什么?

我在代码中发现了一些错误。

currentRound对象的类型是Results。它不是Round对象,直到您检索每个元素。所以它没有holes属性。您应该获取由Results包含的元素,如下所示:

var currentRound = realm.objects('Round').filtered('done == "no"')[0] 

路线插值应该是`id == ${i}`(使用反引号和${})。所以,你的查询应该是:

holesObjects.filtered(`id == ${i}`) 

holesObjects.filtered(`id == ${i}`)回报也Results对象。你应该首先检索一个元素。

realm.write(()=> currentRound.holes.push(holesObjects.filtered(`id == ${i}`)[0])) 

我编辑的整个代码如下:

exportRound =() => { 
    let holesObjects = realm.objects('Hole') 
    console.log(holesObjects); 
    if(holesObjects.length < 9){ 
    alert('Enter stats for at least 9 holes please') 
    } 
    else{ 
    var sortedHoles = holesObjects.sorted('id') 
    var currentRound = realm.objects('Round').filtered('done == "no"')[0] 
    console.log(currentRound) 
    for(var i = 1; i < holesObjects.length; i++){ 
     console.log(holesObjects.filtered(`id == ${i}`)) 
     realm.write(()=> currentRound.holes.push(holesObjects.filtered(`id == ${i}`)[0])) 
    } 
    } 
}