sebelga / nsql-cache

Advanced cache layer for NoSQL databases
MIT License
9 stars 3 forks source link

Cannot read property '0' of null #4

Closed cristyansv closed 6 years ago

cristyansv commented 6 years ago

When I try to make a basic query I get the following error

config:

const cacheConfigRedis = {
    cache: {
      stores: [
        {
          store: redisStore,
          host: '127.0.0.1', // default value
          port: 6379, // default value
        },
      ],
    },
  };

Query: global.db.UserModel.findOne({email}, null, null, {readAll: true});

erorr:

eror here TypeError: Cannot read property '0' of null
    at onResult (/Users/cristyansepulveda/liveprojectbackend/node_modules/nsql-cache/lib/queries.js:196:47)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

i think the error is here: line 196: [unMarshalKeys(resultCached[0]), resultCached[1]]

https://github.com/sebelga/nsql-cache/blob/154b3324f0585733bf84d089d993764f0a1986b8/lib/queries.js#L153-L197

sebelga commented 6 years ago

Hi, Thanks for reporting this! I will have a look as soon as I can. Meanwhile can you try replacing line 154 by

if (resultCached === null || typeof resultCached === 'undefined') {

and let me know if it fixes the issue? thanks :+1:

cristyansv commented 6 years ago

after making this change, i get the following error:

(node:59646) UnhandledPromiseRejectionWarning: Error: Entity Key must be an instance of gcloud Key
    at new Entity (/Users/cristyansepulveda/liveprojectbackend/node_modules/gstore-node/lib/entity.js:24:23)
    at new Model (/Users/cristyansepulveda/liveprojectbackend/node_modules/gstore-node/lib/model.js:39:1)
    at new ModelInstance (/Users/cristyansepulveda/liveprojectbackend/node_modules/gstore-node/lib/model.js:41:31)
    at Function.__model (/Users/cristyansepulveda/liveprojectbackend/node_modules/gstore-node/lib/model.js:878:16)
    at onSuccess (/Users/cristyansepulveda/liveprojectbackend/node_modules/gstore-node/lib/query.js:158:34)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

error is here:

function onSuccess(queryData) {
            const { entities } = queryData;

            if (entities.length === 0) {
                return cb(new GstoreError(
                    errorCodes.ERR_ENTITY_NOT_FOUND,
                    `${Model.entityKind} not found`
                ));
            }

            const [e] = entities;
            const entity = Model.__model(e, null, null, null, e[Model.gstore.ds.KEY]);
            return cb(null, entity);
        }

the javascript object seems not to be converted to the Model

sebelga commented 6 years ago

Ok thanks for the feedback I will look into it asap 👍

cristyansv commented 6 years ago

I'm trying to use this for a project, but I haven't been able to get good results.

many times I update an Entity in one method, but when I call it from another method it brings me a previous version saved in cache.

The only solution I've had is to override the cache on every edition I make.

also the data behaves a little weird, for example if an entity has an id, in some cases it returns the id as a string, and but when it brings the entity from the cache it returns the id as an integer

I'd like to help solve these mistakes, but I'm a little lost where to start.

sebelga commented 6 years ago

Hello, I started looking into the issue, I haven't found exactly the way to reproduce your first issue ("Cannot read property '0' of null") but I saw a bug elsewhere that might have an impact. I still have to make more trials.

You mentioned a few other issues (getting a cached version after an update instead of the latest version & getting a number id instead of a string).

The best would be to have a very small scenario where to reproduce those bugs and see from there. We then need to:

This is what I usually do in a test file. If you can try to create a very small node project (1 file) with just this minimum this would be a great point to start.

Note that by default gstore will convert a string id of numbers ("1234") to a number (1234). If you provide such IDs, then you have to set the Schema option keyType to "name" (https://sebelga.gitbooks.io/gstore-node/content/schema/schema-options.html)

Cheers! :+1:

cristyansv commented 6 years ago

thank you!

right now I'm using the cache version in memory, because I can't get Redis to work.

I will try to make some small examples to better diagnose the problem

sebelga commented 6 years ago

Hi @cristyansv I released a fix for the issue, v1.1.2. Could you try and let me know if it works?

Could you open a new issue for your other problem: "many times I update an Entity in one method, but when I call it from another method it brings me a previous version saved in cache". ? If you could add a simple scenario to reproduce it would help, thanks! :+1:

sebelga commented 6 years ago

Hello, Did you have the chance to test the latest release with Redis client? cheers

Maher4Ever commented 6 years ago

I was having this issue and can confirm that the latest release fixed it for me 🎊

sebelga commented 6 years ago

Great, thanks for the feedback!