distante distante - 1 month ago 15
AngularJS Question

How to wrap cordova getAppVersion on a variable promise?

I have this code:

var promiseVersionNumber;
if (typeof cordova !== 'undefined') {
console.log("I have cordova, I will try to get the AppversionNumeber");
promiseVersionNumber = cordova.getAppVersion.getVersionNumber();
} else {
console.warn("There is no cordova, I will use a dummyAppVersion");
promiseVersionNumber = $q.when(dummyAppVersion);
}

return $q.when(
(promiseVersionNumber).then(function(versionNumber){
defaultConfig.version = versionNumber;
return dbAppInfo.put(defaultConfig);
}).then(function(putDefaultConfig){
console.log("appConfig was created and saved on Database");

return dbAppInfo.put({
_id: 'myLastUpdate',
myLastUpdate: today,
debug: todayDebug
});
}).catch(function(error){
console.error("There was an error while initAppConfig appConfig on Database");
console.dir(error);
console.groupEnd("initAppConfig");
return error;
}));


In browser with my dummy
promiseVersionNumber
all works ok, but on device I get this error:


TypeError: promiseVersionNumber.then(...).then(...).then(...).catch is not a function


How can I correctly wrap
cordova.getAppVersion.getVersionNumber()
?

Answer

Just use ngCordova, that would be the easy way. http://ngcordova.com/docs/plugins/appVersion/