我有一个可以存储和显示一些数据的应用程序。我想将数据存储在Realm中,然后阅读并在每次应用启动时显示。数据添加到一个屏幕中,但显示在另一个屏幕中。
我想拥有一个显示已插入数据列表的主屏幕,以及一个导航到另一个屏幕的按钮,在其中添加新数据。第一次运行它时,它运行正常,但是在我输入第一个数据条目并将其保存到Realm之后,如果再次打开该应用程序,它将显示Realm未定义。
代码看起来像这样:
const Realm = require('realm');
const DataSchema = {
name: 'Person',
properties: {
name: 'string'
}
}
class Homescreen {
async componetDidMount() {
data = [];
Realm.open({schema: [DataSchema]})
.then(realm => {
people = realm.objects('Person');
for(let p in people){
data.push(p.name);
}
.
.
.
}
}
render(){
//render a list with the data from componentDidMount and a button to navigate to the class to add more data
}
}
class AddNewData {
AddNewData(givenName){
Realm.open({schema: [DataSchema]})
.then(realm => {
realm.write(() =>
realm.create('Person', {name: givenName}));
}
}
render(){
//a textInput to add the name and a button(with func AddNewData) to add it to Realm
}
}
The code has some parts cut off, but what I wanted was to, when I open the app again, any person added to the realm will be in the list in the Homescreen, and instead I'm getting "undefined"