我正在使用 MongoDB - 应用服务作为后端构建一个react-native-app。
一切都工作正常,直到我添加了自定义架构对象类型(User_location)。 SDK不想接受它。
这是我的用户模式模型:
import "react-native-get-random-values";
import Realm, { BSON, ObjectSchema } from "realm";
// import { User_location } from "./User_location";
export class User extends Realm.Object<User> {
_id!: BSON.ObjectId;
userName?: string;
firstName?: string;
lastName?: string;
email!: string;
avatar?: string;
userId!: string;
admin!: boolean;
location?: User_location;
static schema: ObjectSchema = {
name: "User",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
userName: "string?",
firstName: "string?",
lastName: "string?",
email: "string",
avatar: "string?",
userId: "string",
admin: "bool",
location: "User_location?",
},
};
}
export class User_location extends Realm.Object {
latitude!: number;
longitude!: number;
static schema: ObjectSchema = {
name: "User_location",
embedded: true,
properties: {
latitude: "double",
longitude: "double",
},
};
}
我得到的错误:
[错误:在架构中找不到对象类型“User_Location”。]
我完全按照此处文档中的示例进行操作: [https://www.mongodb.com/docs/realm/sdk/react-native/model-data/relationships-and-embedded-objects/]
我为 ObjectType 定义了一个类并将其用作 SchemaType,如文档中所示,但它仍然不知道类型。我在这里错过了什么?
您必须将 User_location 架构与其他架构一起传递到 schema 属性中才能正常工作
看起来像这样
<RealmProvider
schema = [...otherSchemas,User_location]
...
//other props
>{children}
</RealmProvider>