onPressFavourites(item) {
let realm = Realm.open({
path: RNFS.DocumentDirectoryPath +'/trees.realm',
schema: [sightingSchema, treeSchema],
schemaVersion: 7,
}).then(realm => {
if(item.favourites === 0) {
realm.write(() => {
item.favourites = 1;
item.favouritesColour = '#91b54d';
});
} else {
realm.write(() => {
item.favourites = 0;
item.favouritesColour = 'transparent';
});
}
});
alert(item.favourites)// Update a property value
}
我正在尝试在单击按钮时更新Realm中的对象但是我收到错误
"可能未处理的承诺拒绝(id:0): 错误:无法在写入事务之外修改托管对象。"
此代码几天前正在运行,但现在抛出了上述错误。
我仍然在学习React Native和Realm,但是根据我的理解以及以下示例和Realm文档,我使用了正确的代码,因此它应该可以工作。
修改
似乎领域和写交易都很好。 我们能够找到一种迂回的方法来修复它,但是现在更新不会显示,直到应用程序刷新为止。
使用参数Possible Unhandled Promise Rejection (id: 0):
Error: Cannot modify managed objects outside of a write transaction.
定义所选的Realm对象时,似乎产生了错误item
。但是,如果我们创建与item
相关的变量,则不会产生该错误。有什么想法会发生这种情况吗?
onPressFavourites(item) {
//console.log(realm);
let realm = Realm.open({
path: RNFS.DocumentDirectoryPath +'/trees.realm',
schema: [sightingSchema, treeSchema],
schemaVersion: 7,
}).then(realm => {
let trees = realm.objects('TreeInfo');
for(var i=0; i<trees.length;i++){
if(trees[i].commonName == item.commonName){
var chosen = trees[i];
break;
}
}
console.log(chosen);
if(item.favourites === 0) {
realm.write(() => {
//item.favourites = 1;
//item.favouritesColour = '#91b54d';
chosen.favourites = 1;
chosen.favouritesColour = '#91b54d';
});
}
else {
realm.write(() => {
chosen.favourites = 0;
chosen.favouritesColour = 'transparent';
});
}
});
alert(item.favourites)// Update a property value
}
修改
render() {
var treeList = this.state.trees;
console.log(treeList);
const {navigate} = this.props.navigation;
//var tree = treeList[0];
return(
<Container>
<Header searchBar style={styles.searchBar}>
<Item style={styles.searchBarInner}>
<Icon name="ios-search" />
<Input placeholder="Search" />
</Item>
</Header>
<List dataArray={treeList}
renderRow={(item) =>
<ListItem style={styles.ListItem} button={true}>
<ListButton item={item}
onSelect={(item) => this.saveTreeInfo(item)}
onSelectFavourites={(item) => this.onPressFavourites(item)
}
/>
</ListItem>
}
>
</List>
</Container>
);
}
以上是将item
传递给onPressFavourites
函数的地方。 item
是从一个领域对象treelist
数组生成的,并显示在列表中。
treelist
数组来自下面显示的变量this.state.trees
。
filterContent(){
Realm.open({
path: RNFS.DocumentDirectoryPath +'/trees.realm',
schema: [sightingSchema, treeSchema],
schemaVersion: 7,
}).then(realm => {
let trees = realm.objects('TreeInfo');
let length = trees.length;
let treeRealm = trees.sorted('commonName');
console.log(this.state.leafEdges);
if (this.state.leafEdges === 'smooth') {
var smoothLeaf = treeRealm.filtered('leafEdges CONTAINS "Smooth"');
this.setState({trees:smoothLeaf});
}
if (this.state.leafEdges === 'toothed') {
var toothedLeaf = treeRealm.filtered('leafEdges CONTAINS "Toothed"');
this.setState({trees:toothedLeaf});
}
if (this.state.leafEdges === 'notsure') {
this.setState({trees:treeRealm});
}
else if (this.state.leafEdges === 'null') {
this.setState({trees:treeRealm});
}
});
}
答案 0 :(得分:2)
用英语回答:
onError
有关于trees
的内容,因此您无法更改它。只需像这样复制一个新数组:
realm
let trees = realm.objects('TreeInfo');
答案 1 :(得分:0)
这个树里面关联了realm里的一些东西,所以不能改变,要重新复制一个数组,像这样
让树= realm.objects(&#39; TreeInfo&#39;);
var arr=[];
for (let i = 0;trees && i < trees .length; i++) {
var item=trees[i];
var item2={
id: item.id,
....
}
arr.push(item2);
}
答案 2 :(得分:0)
你应该改进语法
let order = {...bla, bla,bla}
realm.write(() => {
realm.create("order", order);
});