这些是我的领域对象。我有洞和圆。我试图在一次写作中用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的hole属性。任何帮助将不胜感激。
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}')) )
}
}
}
答案 0 :(得分:0)
你面临的错误是什么?
我在你的代码中发现了一些错误。
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]))
}
}
}