question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Second, identical findAll always returns an empty array

See original GitHub issue

Great project! We as a team are enjoying to work with this.

Description

For us there seems to be a small problem though. The first request goes fine and returns the right response. The second request, with the exact same parameters, however seems to return an array instead of the actual previous request. I’m expecting that this is a cache issue since doing.

I hope you can help us find the cause of this issue.

What I already tried

  • Run clear() before the findAll - fixes the issue but no caching 😦
  • Remove the deserializer from the model - didn’t help at all, same problem

Related code

package.json

{
  "js-data": "^3.0.0-rc.9",
  "js-data-http": "~3.0.0-rc.2"
}

store.js

import { DataStore } from 'js-data';
import { HttpAdapter } from 'js-data-http';
const store = new DataStore();

// Instantiate the HttpAdapter
const adapter = new HttpAdapter({
  basePath: '/v1'
});

// Register the adapter with the Mapper
store.registerAdapter('http', adapter, { 'default': true });

export default store;

user.js

import store from './store';

store.defineMapper('User', {
  endpoint: '/users',
  deserialize (resourceConfig, response) {
    return response.data.user || response.data.users;
  },
  methods: {
    fullName () {
      return this.firstname + ' ' + this.surname;
    }
  }
});
const User = store.as('User');

export default User;

findAll request

import User from 'models/user';
let users;
let meta;

// params.query is a dynamic string based on an search input
let params = {
  per_page: 20,
  page: 1,
  query: null
};

User.findAll(params, { raw: true })
  .then((response) => {
    meta = JSON.parse(response.request.response).meta;
    users = response.data;
  })
  .catch((error) => {
    console.error(error);
  });

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
johman10commented, Apr 4, 2018

@brodie-g’s approach is actually quite nice. I ended up doing something similar in my project. To add up onto that you might have issues with cache if you have a SPA running though, since removing records won’t actually works.

So to add up onto that solution we did this:

import { DataStore } from 'js-data';
import { HttpAdapter } from 'js-data-http';

function setCache (store, mapperName, data, queryHashOrId) {
  return store._completedQueries[mapperName][queryHashOrId] = data;
}

function removeCache (store, mapperName) {
  if (!store._completedQueries) return;
  store._completedQueries[mapperName] = {};
}

// Override the cache methods from the DataStore to just save the plain objects instead of functions.
const store = new DataStore({
  cacheFindAll (mapperName, data, queryHash) {
    return setCache(this, mapperName, data, queryHash);
  },
  cacheFind (mapperName, data, id) {
    return setCache(this, mapperName, data, id);
  }
});

// Invalidate the cache of a mapper as soon as one or more records is changed to make sure a new request will be made to the HTTP server
const adapter = new HttpAdapter({
  afterCreate (mapper) {
    return removeCache(store, mapper.name);
  },
  afterCreateMany (mapper) {
    return removeCache(store, mapper.name);
  },
  afterDestroy (mapper) {
    return removeCache(store, mapper.name);
  },
  afterDestroyAll (mapper) {
    return removeCache(store, mapper.name);
  },
  afterUpdate (mapper) {
    return removeCache(store, mapper.name);
  },
  afterUpdateAll (mapper) {
    return removeCache(store, mapper.name);
  },
  afterUpdateMany (mapper) {
    return removeCache(store, mapper.name);
  }
});

// Link them together
store.registerAdapter('http', adapter, { 'default': true });

We are running this in a production app without any problems by now. Personally I’m all happy with this solution. 😄

3reactions
johman10commented, Mar 17, 2017

@bbthorntz: I think this is something that JSData should do by itself right? In my mind it doesn’t make a whole lot of sense that it suddenly returns an empty array because of cache. If JSData finds a cached response with the same parameters it should return the cached data, not an empty array.

For me in the specific situation described in the issue I was able to use force: true. But I can imagine how this is not always a valid scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Empty list returned from ElementTree findall - Stack Overflow
I'm new to xml parsing and Python so bear with me. I'm using lxml to parse a wiki dump, but I just want...
Read more >
Methods of RegExp and String - The Modern JavaScript Tutorial
If the regexp has flag g , then it returns an array of all matches as strings, without capturing groups and other details....
Read more >
How to clone an array in JavaScript - freeCodeCamp
map returns an array of the same length every single time. To double a list of numbers, use map with a double function....
Read more >
Create empty array of specified class - MATLAB ... - MathWorks
Use this syntax to define an empty array that is the same size as an existing empty array. Pass the values returned by...
Read more >
Underscore.js
Given an array of arrays, returns a series of new arrays, the first of which contains all of the first elements in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found