我有一个js文件,在其中声明了架构,数据库选项和用于插入数据,从架构中查询数据的函数。
我正在android模拟器上运行该应用程序,每当我尝试插入数据时,它总是会返回此错误TypeError:undefined is not a function(evaluating'_realm2.default.open(databaseOptions)')
,任何人都可以帮忙,我已经坚持了好几天了((>
ALLSCHEMAS.JS
import Realm from 'realm';
export const PERSON_SCHEMA = "Person";
export const PersonSchema = {
name: PERSON_SCHEMA,
primaryKey: 'id',
properties: {
id: 'int',
name: {type: 'string', indexed: true}
}
};
const databaseOptions = {
path: 'person.realm',
schema: [PersonSchema],
schemaVersion: 0
};
export const insertNewPerson = newPerson => new Promise((resolve, reject)=>{
Realm.open(databaseOptions).then(realm => {
realm.write(() => {
realm.create(PERSON_SCHEMA, newPerson);
resolve(newPerson);
});
}).catch((error) => reject(error));
});
export const queryAll = () => new Promise((resolve, reject) => {
Realm.open(databaseOptions).then(realm => {
let allPersons = realm.objects(PERSON_SCHEMA);
resolve(allPersons);
}).catch((error) => {
reject(error);
});
});
export default new Realm(databaseOptions);
APP.JS
import React from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {insertNewPerson, queryAll} from "./databases/allSchemas";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {timePassed: false, value: '', realm: "books"};
}
render() {
const newPerson = {
id: Math.floor(Date.now() / 1000),
name: this.state.realm
};
insertNewPerson(newPerson).then().catch((error) => {
alert(error);
});
queryAll().then((value) => {
this.setState({value});
}).catch((error) => {
})
return (
<View>
<Text>
{this.state.value}
</Text>
</View>
);
}
}