4

thanks in advance

Iam using watermelon db in react native android it returns undefined for the collection.

 const handleDB = async () => {
    const myCollection = database.collections.get('data_items')

    await database.action(async () => {
      const newPost = await myCollection.create(post => {
        post.ItemID = 9
        post.ItemParentItem_ID = 8
      })
    })
  }

And i call this from same Functional Component

handleDB()

Is there any work around

My DB

import { Database } from "@nozbe/watermelondb";
import SQLiteAdapter from "@nozbe/watermelondb/adapters/sqlite";
import { dbModel } from "./"
import { mySchema} from "./schema"

const adapter = new SQLiteAdapter({
    dbName: 'Northel',
    schema: mySchema
});

export const database = new Database({
    adapter,
    modelClasses: [dbModel],
    actionsEnabled: true,
});

If I console log myCollection I get undefined.

i have imported the database as

import { database } from '../model/database'
Ankit Jayaprakash
  • 1,040
  • 3
  • 15
  • 31
  • This issue also appears when you do net correctly extend your model from Model. in our case we accidentally used `implements` instead of `extends`. Maybe it spares someone else a painstaking hours long search. – Arne Fischer Aug 06 '21 at 14:22
  • Did you ever find out what caused this? I'm running into it as well but I definitely have WatermelonDB set up correctly as I'm able to write to the database but my reads are returning undefined. – Ryker Aug 31 '21 at 21:25

3 Answers3

1

The same thing happened to me. Make sure you have completely configured the database, in my case, forget to add the Model to modelClasses [].

Omar
  • 11
  • 1
0

Have you defined your models, schemas and relationships correctly? See how here: Schema, Model and Relation.

If everything is fine, did you add this table later to the database and not update the schema version? If so, update the schema version and reinstall your mobile application.

If that doesn't work, share your model and schema definitions, it will help to identify the problem.

Raffa
  • 58
  • 7
-1

This usually happens when you have not named the model correctly or imported the model in the database setup