Start a new topic
Answered

How to fetch/read data?

Hello

I'm bloody new to Kinvey just trying out fetching data from a collection (with some sample records).


I posted a code snippet together that is working fine so far but I just can't find anything in the description HOW to receive/fetch the data from the collection. My sample so far:


$(document).ready(function () {

Kinvey.init({
appKey: '<myappkey>',
appSecret: '<myappsecret>'
});

var promise = Kinvey.User.login('Tayger', 'tiger1971')
.then(function(user) {
// Fetch game data
var dataStore = Kinvey.DataStore.collection('games');
var query = new Kinvey.Query();
query.equalTo('gameid', '12345');

var stream = dataStore.find(query);

stream.subscribe(function onNext(entities) {
// ...
}, function onError(error) {
// ...
}, function onComplete() {
fetch collection data probably here?
});

})
.
catch(function(error) {
console.log (error);
});

});

Login works, user is granted to the collection 'games', no error message, ... How can I now fetch/receive the returning data?


Regards




Best Answer

Hello Tayger,


The Kinvey JavaScript Library uses the observable pattern to return data on find  operations. APIs that fetch data require the application to register a  subscriber that gets invoked when data is available. We use Observables to accomplish this.


The subscribe function accepts three arguments: the first is a function invoked when new data is available, the second is a function invoked on error, and the third is a function invoked when the asychronous process is complete. Please take a look at this link for better understanding. Please try printing 'entities' in onNext function.


You can also transform an observable into a promise. Check this link for doing that. Also, please take a look at our sample app/ project on this link. Starter app to help you get started with building your HTML5 app on the Kinvey Platform



Thanks,

Pranav

Kinvey

1 Comment

Answer

Hello Tayger,


The Kinvey JavaScript Library uses the observable pattern to return data on find  operations. APIs that fetch data require the application to register a  subscriber that gets invoked when data is available. We use Observables to accomplish this.


The subscribe function accepts three arguments: the first is a function invoked when new data is available, the second is a function invoked on error, and the third is a function invoked when the asychronous process is complete. Please take a look at this link for better understanding. Please try printing 'entities' in onNext function.


You can also transform an observable into a promise. Check this link for doing that. Also, please take a look at our sample app/ project on this link. Starter app to help you get started with building your HTML5 app on the Kinvey Platform



Thanks,

Pranav

Kinvey

Login or Signup to post a comment