React Native Realm:构造函数没有在这个Realm的模式中注册



我遵循这个https://www.mongodb.com/docs/realm/sdk/react-native/use-realm-react/尝试在一个简单的任务应用程序中设置我的数据库。我得到一个顽固的错误"渲染错误:构造函数未在此real& quot;模式中注册。在我尝试向上下文添加第二个模式后,这种情况开始发生,所以我删除了它,并将我已经很简单的应用程序剥离回原来的样子。我在网上找不到关于这个错误的任何东西,Realm文档也没有多大帮助。似乎没有太多的教程或资源来获得一个Realm应用程序设置。

AppWrapper.js

import React from "react";
import { SafeAreaView, View, Text, TextInput, FlatList, Pressable } from "react-native";
import RealmContext from "./src/schemas/ContextCreation";
import Task from "./src/schemas/Task";
const { RealmProvider } = RealmContext;
export default function AppWrapper() {
return (
<RealmProvider>
<TaskApp/>
</RealmProvider>
);
}
const TaskApp = () => {
const { useRealm, useQuery, useObject } = RealmContext;
const realm = useRealm();
const tasks = useQuery(Task);
const [newDescription, setNewDescription] = React.useState("");
return (
<SafeAreaView>
<View style={{ flexDirection: 'row', justifyContent: 'center', margin: 10 }}>
<TextInput
value={newDescription}
placeholder="Enter new task description"
onChangeText={setNewDescription}
/>
<Pressable
onPress={() => {
realm.write(() => {
realm.create("Task", Task.generate(newDescription));
});
setNewDescription("");
}} ><Text>➕</Text>
</Pressable>
</View>
<FlatList data={tasks.sorted("createdAt")} keyExtractor={(item) => item._id.toHexString()} renderItem={({ item }) => {
return (
<View style={{ flexDirection: 'row', justifyContent: 'center', margin: 10 }}>
<Pressable
onPress={() =>
realm.write(() => {
item.isComplete = !item.isComplete
})
} ><Text>{item.isComplete ? "✅" : "☑️"}</Text>
</Pressable>
<Text style={{ paddingHorizontal: 10 }} >{item.description}</Text>
<Pressable
onPress={() => {
realm.write(() => {
realm.delete(item);
});
}} ><Text>{"🗑️"}</Text>
</Pressable>
</View>
);
}} ></FlatList>
</SafeAreaView >
);
}

Task.ts

export default class Task extends Realm.Object {
_id!: Realm.BSON.ObjectId;
description!: string;
isComplete!: boolean;
createdAt!: Date;
static generate(description: string) {
return {
_id: new Realm.BSON.ObjectId(),
description: description,
createdAt: new Date(),
};
}
static schema = {
name: 'Task',
primaryKey: '_id',
properties: {
_id: 'objectId',
description: 'string',
isComplete: { type: 'bool', default: false },
createdAt: 'date'
},
};
}

ContextCreation.ts

import { createRealmContext } from "@realm/react";
import Task from "./Task";
const config = {
schema: [Task.schema],
};
export default createRealmContext(config);

当我发现这些资源时,我正要放弃使用realm:

使用世博会
  • 打印稿:https://github.com/realm/realm-js/tree/master/templates/expo-template-ts
  • JavaScript: https://github.com/realm/realm-js/tree/master/templates/expo-template-js
React Native Cli
  • 打印稿:https://github.com/realm/realm-js/tree/master/templates/react-native-template-realm-ts
  • JavaScript: https://github.com/realm/realm-js/tree/master/templates/react-native-template-realm-js

我的意见:你可以简单地像这样生成一个新的Typescript项目:npx react-native init AwesomeRealmProject --template @realm/react-native-template-ts这个命令将生成一个简单的todo应用…玩一下代码,它会给你更多的理解如何正确设置领域…

此错误显示为up,因为realm无法在其模式中为您请求的对象类型找到合适的构造函数。

你需要把你的Task类传递给你的config.schema数组而不是你在ContextCreation.ts:5:12所做的Task.schema,例如

const config = {
schema: [Task],
};

必须在模式类中创建构造函数

constructor(realm: Realm, room: string, role: string) {
super(realm, {
room,
role
});
}

相关内容

  • 没有找到相关文章

最新更新