Sven Hager
Sven Hager

Reputation: 64

MongoDB Atlas Device Sync (App Services) - React Native

i am building a react-native-app with MongoDB - App Services as backend.

Everything worked fine already, until i added a custom schema object type (User_location). The SDK don't want to accept it.

Here is my schema-model for a User:

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",
    },
  };
}

The error i get:

[Error: Object type 'User_Location' not found in schema.]

I followed exactly the example in the docs here: [https://www.mongodb.com/docs/realm/sdk/react-native/model-data/relationships-and-embedded-objects/]

I defined a class for the ObjectType and used it as a SchemaType, as shown in the docs, but it still doesn't know the type. What did i miss here?

Upvotes: 0

Views: 170

Answers (1)

Albert Yumnam
Albert Yumnam

Reputation: 1

You have to pass your User_location schema into the schema prop along with the other schemas in order for this to work

it'd look something like this

<RealmProvider
schema = [...otherSchemas,User_location]
...
//other props
>{children}
</RealmProvider>

Upvotes: 0

Related Questions