gpt4 book ai didi

javascript - 无法在 Realm + React Native 中修改写入事务错误之外的托管对象

转载 作者:行者123 更新时间:2023-11-30 14:48:59 25 4
gpt4 key购买 nike

我正在尝试在 Realm 和 React Native 中为我的数据库编写一个类,我确定出了什么问题。我的 Realm 代码在下面。当我在主题管理器类上调用 createTopic 方法时,我收到一条错误消息“无法在写入事务之外修改托管对象”。在我的屏幕上显示为红色。写入数据库的方法以前是有效的……所以我一定是遗漏了一些简单的东西。感谢您的关注!

import React, { Component } from 'react';

const Realm = require('realm');

class Topic {}
Topic.schema = {
name: 'Topic',
primaryKey: 'key',
properties: {
name: 'string',
key: 'string'
},
};

const topicManager = new Realm({schema: [Topic]});


export class TopicManager {

constructor(props) {

}

createTopic(insertName) {
const uuid = this.uuidv4();
savedTopic = topicManager.create('Topic', {
key: uuid,
name: insertName,
});
}

uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

}

最佳答案

可能

createTopic(insertName) {
topicManager.write(() => { // <--
savedTopic = topicManager.create('Topic', {
key: uuid,
name: insertName,
});
}); // <--
}

关于javascript - 无法在 Realm + React Native 中修改写入事务错误之外的托管对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48452165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com