React Native watermelon db collection returns undefined

2.6k views Asked by At

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'
3

There are 3 answers

0
Samir Prajapati On

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

0
Raffa On

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.

0
Omar On

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