Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

TypeError: undefined is not an object (evaluating 'db.schema.tables') #1119

Open hugo0991 opened 3 years ago

hugo0991 commented 3 years ago

I tried everything, this is my environment:

"@nozbe/watermelondb": "^0.23.0", "@nozbe/with-observables": "^1.4.0",

"devDependencies": { "@babel/plugin-proposal-decorators": "^7.14.5",

.....................................................................................................................................................................

this is happening when I added a Model to the modelClasses object

here my code ........ (Index.js)

import {AppRegistry} from 'react-native'; import App from './App'; import {name as appName} from './app.json';

import { Database } from "@nozbe/watermelondb"; import SQLiteAdapter from "@nozbe/watermelondb/adapters/sqlite";

import { schema } from "./models/schema"; import { migrations } from "./models/migrations"; import Task from './models/Task'

const adapter = new SQLiteAdapter({ dbName: 'ReactNativeDB', schema, migrations, //jsi:true, onSetUpError: error => { // Database failed to load -- offer the user to reload the app or log out } });

const database = new Database({ adapter, modelClasses: [Task], });

AppRegistry.registerComponent(appName, () => App);

I tried 10 different solutions and examples, and I'm still getting the same issue in iOS and Android, please advice

.....................................................................................................................................................................Scheme

import { appSchema, tableSchema } from '@nozbe/watermelondb'

export default mySchema = appSchema({ version: 1, // Used for migrations tables: [ tableSchema({ name: 'tasks', // Convention is to be plural lowercase of the model columns: [ { name: 'description', type: 'string' }, { name: 'is_complete', type: 'boolean' }, // These will be dates but we define it as numbers { name: 'created_at', type: 'number' }, { name: 'updated_at', type: 'number' } ] }), tableSchema({ name: 'subtasks', columns: [ { name: 'description', type: 'string' }, { name: 'is_complete', type: 'boolean' }, // This is a foreign key but we define it as a string { name: 'task_id', type: 'string', isIndexed: true }, // These will be dates but we define it as numbers { name: 'created_at', type: 'number'}, { name: 'updated_at', type: 'number'} ] }), ] })

..................................................................................................................................................................... Model

import { Model } from '@nozbe/watermelondb' import { field, date, readonly, action } from '@nozbe/watermelondb/decorators'

// Naming convention is CamelCase singular export default class Task extends Model { static table = 'tasks' static associations = { subtasks: { type: 'has_many', foreignKey: 'task_id' }, }

// These are our own fields @field('description') description @field('is_complete') isComplete

// These are special fields that will automatically update when the // record is created and updated @readonly @date('created_at') createdAt @readonly @date('updated_at') updatedAt

// Actions are functions that you can call on the database object // These can be something like calculating a new field, but in this // case we're using them to modify the database object directly. @action async addSubtask(description) { return this.collections.get('subtasks').create(subtask => { subtask.description = description subtask.isComplete = false }) }

@action async rename(newName) { await this.update(t => { t.description = newName }) }

@action async delete() { await this.markAsDeleted() } }

anchetaWern commented 3 years ago

same issue. @hugo0991 did you manage to find a solution?

tonygomez commented 2 years ago

we are having the same issue. @hugo0991 or @anchetaWern did you manage to find a solution?

ringkubd commented 2 years ago

Same issue here... @tonygomez @anchetaWern @hugo0991 did you manage to find a solution?

tonygomez commented 2 years ago

@ringkubd yes, in my case I switched from using @text to @field in my model definition and it started working. I did not have time to dig into the details but chalked it up to some implicit data type conversion failing to text. We have been using watermelondb in production now for several months and it has been working as advertised, great technology!