Start a new topic

How to avoid error message on empty datastore?

Hello


I want to read from an empty datastore. This is a valid situation in my project case.

I tried both ways provided in the tutorials:


find()

var dataStore = Kinvey.DataStore.collection('geospotter');
var query = new Kinvey.Query();
query.equalTo('_id', activeUser._id);
var stream = dataStore.find(query);
stream.subscribe(function onNext(cloud) {
// ...
}, function onError(error) {
console.log (error);
}, function onComplete() {
console.log ("passed");
});


findById()

var stream = dataStore.findById(activeUser._id);
stream.subscribe(function onNext(cloud) {
// ...
}, function onError(error) {
console.log (error);
}, function onComplete() {
console.log ("passed");
});


In both cases I jumps into the onError function showing this error message:

code: undefined


debug: undefined

kinveyRequestId: undefined

message: "Unable to find the entity on the backend. There is 1 entity, matching the provided query or id, pending push to the backend. The resul…"

name: "KinveyError"

stack: "o@https://<myUrl>/kinvey-html5-sdk-3.12.2.min.js:8:23916…"


Since the dataStore is empty there can't be any matching result in the backend(!)


How can I tell Kinvey it's ok if there are no data and I will take of it in such a case? I just try to avoid that Kinvey means this is an error.


Regards




1 Comment

In between I've found an ugly solution:


1. Define your dataStore this way:

var dataStore = Kinvey.DataStore.collection('<collectionname>', Kinvey.DataStoreType.Network)

( I know DataStoreType.Network is not recommended but it's the only proper way I've found to handle the error coming from backend)


2. If there are no data found on backend it will jump into the onError function (which is completely wrong in my eyes because it's just ok that there are no data in the backend!): Create/insert a dummy record into your collection and reprocess with step 1. This time it will not run into that error.


Regards


Login or Signup to post a comment