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

rxdb addCollections err TypeError: Cannot read properties of null (reading 'hasOwnProperty') #4055

Closed
Albert-Gao opened this issue Oct 12, 2022 · 11 comments · Fixed by #4059
Closed

Comments

@Albert-Gao
Copy link
Contributor

for the following schema:

import { RxJsonSchema } from "rxdb";
import { UserDocType } from "./types";

export const userSchema: RxJsonSchema<UserDocType> = {
  title: "user schema",
  description: "describes a user",
  type: "object",
  version: 0,

  // re-enable once not use dexie.js engine for the web since it does not support it
  keyCompression: false,

  primaryKey: "id",
  properties: {
    id: {
      type: "string",
      maxLength: 100,
    },
    birthday: {
      type: "string",
      default: null,
    },
    timezone: {
      type: "string",
      default: null,
    },
    createTime: {
      type: "string",
    },
    updateTime: {
      type: "string",
    },
  },
  required: ["id", "createTime", "updateTime"],
};

I get an error when creating the database, remove the usage of devplugin "solves" it...

wonder if I am missing something or a bug.

a reproducible repo here:

https://github.com/Albert-Gao/my-rxdb-app

Digging

With some debug, I found the issue here in this function,
image

You can see the runtime value for schemaObj is "string" which is not an object, thus causes the problem, do not know the cause though.

@pubkey
Copy link
Owner

pubkey commented Oct 12, 2022

Your schema looks ok, this should work.

@Albert-Gao
Copy link
Contributor Author

Albert-Gao commented Oct 12, 2022

Quick fix

image

added some guard, which seems to solve the issue, can send a PR if you think it's a solution, I added the isSchemaObj guard and a nullable guard at line 162 in the traverse() function

More

I think the code can be simplified by adding a top-level isSchemaObj guard at the traverse() function, which is the caller of the checkField() function, but I do not know if there is any check which expects the schemaObj, not an object.

This is the simplified version:
image

only touched line 160 and line 167

@pubkey
Copy link
Owner

pubkey commented Oct 13, 2022

You should start with a unit test that reproduces the problem

@Albert-Gao
Copy link
Contributor Author

I created a repo for reproducing it. 😄
and did some quick hack for unblocking. 😄

For a former PR, I cloned the repom, npm i and executed the tests via npm run test, the tests failed with the error Blob is not defined :D I am using Node v16.15.1, what am I missing here? Thanks 😄

image

@pubkey
Copy link
Owner

pubkey commented Oct 13, 2022

You need at least node 18. https://rxdb.info/releases/13.0.0.html

@Albert-Gao
Copy link
Contributor Author

Thanks for the help, progress made, stuck with this error

image

The port 5000 is being used by control center, which is a Mac OS system app, I tried killing it, but not work, it always restarts itself right after killing.

image

I hard-coded the server-couchbase.ts to serve at another port, but having this same problem on another port 🤣

image

What am I missing here? thanks :)

@pubkey
Copy link
Owner

pubkey commented Oct 13, 2022

I fixed it to no longer use hard coded port numbers.
I have no idea about your second error :(

@Albert-Gao
Copy link
Contributor Author

thanks, it sorts of working!

image

is this a test that tied to my local browser settings?

@Albert-Gao
Copy link
Contributor Author

how to run only a single test file please?

@Albert-Gao
Copy link
Contributor Author

Albert-Gao commented Oct 14, 2022

I removed all the other tests and added this one, now it can be reproduced 😀

/**
 * this is a template for a test.
 * If you found a bug, edit this test to reproduce it
 * and than make a pull-request with that failing test.
 * The maintainer will later move your test to the correct possition in the test-suite.
 *
 * To run this test do:
 * - 'npm run test:node' so it runs in nodejs
 * - 'npm run test:browser' so it runs in the browser
 */
import { createRxDatabase, addRxPlugin } from '../../';
import { RxDBDevModePlugin } from '../../plugins/dev-mode';
import config from './config';

describe('TypeError: Cannot read properties of null (reading hasOwnProperty)', () => {
    it('should fail because it reproduces the bug', async () => {
        // create a schema
        const userSchema = {
            title: 'user schema',
            description: 'describes a user',
            type: 'object',
            version: 0,

            // re-enable once not use dexie.js engine for the web since it does not support it
            keyCompression: false,

            primaryKey: 'id',
            properties: {
                id: {
                    type: 'string',
                    maxLength: 100,
                },
                birthday: {
                    type: ['string', 'null'],
                    default: null,
                },
                createTime: {
                    type: 'string',
                },
            },
            required: ['id', 'createTime', 'updateTime'],
        };

        // generate a random database-name
        const name = 'test-app';

        addRxPlugin(RxDBDevModePlugin);

        // create a database
        const db = await createRxDatabase({
            name,
            /**
             * By calling config.storage.getStorage(),
             * we can ensure that all variations of RxStorage are tested in the CI.
             */
            storage: config.storage.getStorage(),
        });

        // create a collection
        await db.addCollections({
            users: {
                schema: userSchema,
            },
        });
    });
});

@Albert-Gao
Copy link
Contributor Author

image

Albert-Gao added a commit to Albert-Gao/rxdb that referenced this issue Oct 14, 2022
pubkey pushed a commit that referenced this issue Oct 15, 2022
…reading hasOwnProperty) (#4059)

* FIX #4055

* tweaked the text

* added test

* added fix

* added fix

* fixed the test

* tweaked the test

* reset the compiled code to the pre-PR state to fix the CI

* tweaked the test

* tweaked the code according to the comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants