Watermelondb: [Tutorial] Cannot read property 'initialize' of undefined

Created on 24 May 2020  Â·  7Comments  Â·  Source: Nozbe/WatermelonDB

I followed the instructions for creating the db tutorial. I got the following error
Unhandled JS Exception: TypeError: Cannot read property 'initialize' of undefined. Thinking I did something wrong, I recreated it. Same error. I've scoured the internet for a solution without success.

My environment
package.json

{
  "name": "movie_directory",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@nozbe/watermelondb": "^0.16.1",
    "@nozbe/with-observables": "^1.0.5",
    "native-base": "^2.13.12",
    "rambdax": "^3.7.0",
    "react": "16.11.0",
    "react-native": "0.62.2",
    "react-native-fullwidth-image": "^0.1.3",
    "react-native-gesture-handler": "^1.6.1",
    "react-navigation": "^4.3.9"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/plugin-proposal-decorators": "^7.8.3",
    "@babel/runtime": "^7.6.2",
    "@react-native-community/eslint-config": "^0.0.5",
    "babel-jest": "^24.9.0",
    "eslint": "^6.5.1",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-test-renderer": "16.11.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

The error occurs in the index.js file when it can't initialize the SQLiteAdapter. Is there any known issues with using Watermelon with the latest react-native version? This is a blocker because the app does not compile.

Here is the index.js file
// 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 {mySchema} from './src/models/schema';
import {dbModels} from './src/models/index.js';

// First, create the adapter to the underlying database:
const adapter = new SQLiteAdapter({
  dbName: 'WatermelonDemo',
  schema: mySchema,
});

// Then, make a Watermelon database from it!
const database = new Database({
  adapter,
  modelClasses: dbModels,
});

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

All 7 comments

Are you already injecting the database to the App component?

I’m attempting to create a working demo based on a tutorial using the code I posted. I’ve been unsuccessful. The app crashes before it can load App.js.

Thanks, @jdnichollsc, your demo may come in handy.

I'm reopening because the issue still exists when working from a fresh install. Thanks, @jdnichollsc for assistance. But your app is written in Typescript which I don't use. I followed the tutorial verbatim except for passing the database object to the navigation handler. I don't need it for this PoC. Also, I factored db ops into it's own component.

Database.js

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

import {mySchema} from '../models/schema';
import {Post, Comment} from '../models/Post';

// First, create the adapter to the underlying database:
const adapter = new SQLiteAdapter({
  mySchema,
});

// Then, make a Watermelon database from it!
const HomeeDb = new Database({
  adapter,
  modelClasses: [Post, Comment],
  actionsEnabled: true,
});

export default HomeeDb;

I import this into App.js with import {HomeeDb} from './services/Database'; Then call it with const db = HomeeDb();. The app crashes when trying to initialize the adapter. I tried to step through it but it's crashing in the node module.

I thought that maybe the problem was in not manually linking the cocoa pod. I tried linking it through Xcode. Same error. I manually unlinked and re-linked through CLI and reran. Doing that caused wall of error text.

Finally, I manually unlinked and let RN auto-link it with pod install. I received the same message [!] use_native_modules! skipped the react-native dependency '@nozbe/watermelondb'. No podspec file was found. Launching the app returns me back to the original undefined error.

I'm out of ideas.

UPDATE:
When I run the app through Xcode, I get the following error
2020-06-04 13:48:02.728 [fatal][tid:com.facebook.react.ExceptionsManagerQueue] Unhandled JS Exception: TypeError: null is not an object (evaluating 'NativeDatabaseBridge[methodName]') Launching from CLI produces the original error.

I stepped through the code into the watermelondb/adapters/sqlite module itself. That is where the error is thrown. This is the log output for the adapter

function SQLiteAdapter(options) { … }
index.js:456

[[FunctionLocation]]:internal#location
[[Scopes]]:Scopes[3]
0:Closure {SQLiteAdapter: }
SQLiteAdapter:function SQLiteAdapter(options) { … }
[[FunctionLocation]]:internal#location
[[Scopes]]:Scopes[3]

arguments:TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

caller:TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
length:1

name:"SQLiteAdapter"
prototype:Object {_getDispatcherType: , testClone: , _getName: , …}
__proto__:function () { … }
1:Closure {_dependencyMap: Array(11), _$$_REQUIRE: , global: global, …}
2:Global {global: global, clearInterval: <accessor>, clearTimeout: , …}
arguments:TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

caller:TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
length:1
name:"SQLiteAdapter"

So if I'm reading this correctly, there's an error reading the params I pass into the constructor. I could really use a quick response on this. I've been spinning on it for days.

I've continued to poke around. I found that I missed a step when manually linking the Xcode lib file. I linked the libWatermelonDB.a and reran the app. It appears to have fixed the original issue. Of course, it uncovered a different issue that was blocked by this. This was a painful lesson that I hope someone else can learn from. Now it's time to progress to the next bug.

I really hope someone with Cocoapods experience can fix this module to allow it to auto-link. It would've made all this trouble unnecessary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bsr203 picture bsr203  Â·  6Comments

impactcolor picture impactcolor  Â·  4Comments

FabianEscarate picture FabianEscarate  Â·  6Comments

markorusic picture markorusic  Â·  4Comments

K4stor picture K4stor  Â·  7Comments