我有2個領域數據模型。 「食物」模型具有對象屬性類型'foodRating',旨在成爲FoodRatings模型的鏈接。Realm React Native - 如何分配對象屬性類型
class FoodRatings extends Realm.Object {}
FoodRatings.schema = {
name: 'FoodRatings',
primaryKey: 'foodRatingId',
properties: {
foodRatingId: { type: 'int', indexed: true },
name: 'string',
}
};
class Foods extends Realm.Object {}
Foods.schema = {
name: 'Foods',
primaryKey: 'foodId',
properties: {
foodId: { type: 'int', indexed: true },
name: 'string',
foodRating: {type: 'FoodRatings'},
}
};
FoodRatings預先填充了許多旨在靜態且不變的行。您從FoodRatings的某個選項中爲食物分配foodRating。
我遇到的問題是當我嘗試創建一個新的食物對象併爲FoodRatings對象分配一個FoodRatings對象時,它會中斷。
foodRatings = realm.objects('FoodRatings')
let foodRating = foodRatings.filtered('foodRatingId = 1');
realm.write(() => {
realm.create('Foods', {foodId: 1, name: 'apples', foodRating: foodRating[0]});
})
我看到很多關於領域的「連接」的討論,但它尚未發佈到領域js。所有示例和文檔假設新條目而不是您要分配給模型的現有對象。
任何想法我可以做到這一點?