Start a new topic
Answered

Two Returns on Datastore fetch

I am working on building a HTML5 app. I am getting both a GET request and OPTIONS request back when I am trying to do a simple fetch of a data store. 


Best Answer

Tayger,


The Cache mode is the default mode for the data store if a type is not explicitly set. Please check this link for more information.


You are getting 2 results because one is from the cache and the other one is from backend. When you are calling find() method, SDK fetches entities from both the cache and the backend. This is working as per the design. The completion handler block will be called twice. 


#1 call: will return the cached data stored locally - Here you are getting empty array because what you are looking for is not present in the cache.


#2 call: will return the data retrieved from the network - Here you are getting an array with searched data because this is fetched from the backend.


Please take a look at different datastore types and select the one which is the most suitable for your usecase.



Thanks,

Pranav

Kinvey




1 person has this question

Hi Pranav


Thank you a lot for this (and all other answers) I have posted, all very helpfull! I have tried many (big) cloud services so far and I can tell you: Kinvey offers the best easy-to-use and most flexible environment! 

1). PHP app
2). I am getting no errors back,-- just a option and get response 
3). 3.3.1

Example of my fetch code:

   

dow = dow;

            var allFetchedAvail = [];
            var availToday = [];
            var dataStore = Kinvey.DataStore.collection('avail');
            var stream = dataStore.find();

            stream.subscribe(function onNext(avail) {
                console.log('fetching');
                allFetchedAvail.push(avail);

                for (i = 0; i < avail.length; i++) {
                    if (avail[i].dayOfWeek !== dow) {
                        var employeeName = avail[i].user_id.name;
                        $('#name').append('<option val="' + employeeName + '">' + employeeName + '</option>');
                    }
                    // Does employee have partial day availabity
                    else if (avail[i].allDay === false) {
                        // var start   = avail[i].start;
                        // var end     = avail[i].end;
                        // console.log(start , end);
                    }
                }
            }, function onError(error) {
                console.error(error);
            }, function onComplete(entity) {
            });

   

Tayger,


Thank you for the kind words. I am glad to hear that you found my answers helpful. Let us know if you have any other questions regarding Kinvey.


Thanks,

Pranav

Kinvey

Nate,

  1. Can you let me know the kid of the HTML5 app that you are building?
  2. Are there any errors you are facing while fetching the data? A screenshot of the same would help me to better understand the issue.
  3. What version of the Kinvey SDK you are using?

 

Thanks,

Pranav

Kinvey Support

I'm having the same problem, 1 year later (latest Kinvey release under Kinvey HTML ):


<script src="https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.10.2.min.js"></script>


...
var
promise = Kinvey.User.login('<username>', '<password>')
.then(function() {
// Fetch games
var dataStore = Kinvey.DataStore.collection('games');

// Define query
var query = new Kinvey.Query();
query.equalTo('gameid', 12345);

var stream = dataStore.find(query);

console.log ("Before"); // Appears once
stream.subscribe(function onNext(entities) {
console.log ("get data"); // Appears twice
console.log (entities); (( Appears twice ((1. empty array, 2. filled array)
}, function onError(error) {
// ...
}, function onComplete() {
var promise2 = Kinvey.User.logout()
.then(function() {
// ...
}).catch(function(error) {
// ...
});
});
})
.catch(function(error) {
console.log (error);
});
});


Any idea why I got two return results? 

1. empty array

2. array with searched data




Answer

Tayger,


The Cache mode is the default mode for the data store if a type is not explicitly set. Please check this link for more information.


You are getting 2 results because one is from the cache and the other one is from backend. When you are calling find() method, SDK fetches entities from both the cache and the backend. This is working as per the design. The completion handler block will be called twice. 


#1 call: will return the cached data stored locally - Here you are getting empty array because what you are looking for is not present in the cache.


#2 call: will return the data retrieved from the network - Here you are getting an array with searched data because this is fetched from the backend.


Please take a look at different datastore types and select the one which is the most suitable for your usecase.



Thanks,

Pranav

Kinvey



Login or Signup to post a comment