2016-06-07 83 views
0

這些是我的境界對象。我有洞和圓。我試圖在一次寫入中填充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}'))) 
    } 
    } 
} 

回答

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])) 
    } 
    } 
}