Start a new topic

Error Kinvey.getActiveUser after Kinvey.init

Hi,



I just start using Kinvey in my Titanium app. I follow all the instruction but I always have this error showing up. I don't know why, here is my code :





// Import the Kinvey module.

var Kinvey = require('kinvey-titanium-1.1.9');



// Initialize Kinvey.

Kinvey.init({

appKey : 'xxx',

appSecret : 'xxx'

});



var promise = Kinvey.ping();

promise.then(function(response) {

console.log('Kinvey Ping Success. Kinvey Service is alive, version: ' + response.version + ', response: ' + response.kinvey);

}, function(error) {

console.log('Kinvey Ping Failed. Response: ' + error.description);

});



For this code I get :



`[ERROR] : V8Exception: Exception occurred at kinvey-titanium-1.1.9.js:267:

Uncaught Kinvey.Error: Kinvey.getActiveUser can only be called after the promise returned by Kinvey.init fulfills or rejects.`







Can you confirm me that appKey = APP ID and appSecret = APP SECRET from the top-right of my app on https://console.kinvey.com ?



Thanks

The problem sesms to be from ping when there is no user logged in or never had user before.

I was able to create user (and so to validate the communication between Kinvey and my app), then login then use ping without problem.
Hello there StephaneJ,



The reason that you're running into this issue is because kinvey.init is asynchronous. Essentially you are calling something that takes some time to do. Javascript is single threaded and plows forward in execution regardless of if the item before it has finished or not. In this case you're getting to kinvey.ping before kinvey.init has finished which is causing your issue.



The way to handle this would be to add a callback to the kinvey.init function.



Thanks,
Nope, don't do callback, the whole point of Promise is to free us from callbacks. Do something like this:





Kinvey.init({

appKey : 'xxx',

appSecret : 'xxx'

}).then(function() {



var promise = Kinvey.ping();

promise.then(function(response) {

console.log('Kinvey Ping Success. Kinvey Service is alive, version: ' + response.version + ', response: ' + response.kinvey);

}, function(error) {

console.log('Kinvey Ping Failed. Response: ' + error.description);

});



});

Thank you for your answer I will try it !
Login or Signup to post a comment