Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hugo0991 opened this issue Jul 26, 2021 · 4 comments
Open

Comments

@hugo0991
Copy link

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
Copy link

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

@tonygomez
Copy link

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

@ringkubd
Copy link

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

@tonygomez
Copy link

tonygomez commented Jun 14, 2022

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants