Parse cloud function - code ot executed in sequence - javascript

I have below function to update rows if meeting some condition and at the end of for loop, response include how many rows updated.Despite more than zero rows updated, response shows zero. Looking at log, it seems reponse.success() fires before completing for loop.
why so?
Parse.Cloud.define("reset", function(request, response) {
var isSaveNeeded = false
var Query = new Parse.Query("price");
Query.equalTo('isActive', true);
Query.find({useMasterKey:true})
.then((results) => {
console.log("Found " + results.length + " price rows")
var currentDate = moment()
var noOfRowsUpdated = 0
for (let i = 0; i < results.length; ++i) {
var valid_till_date = results[i].get('valid_till_date');
if (valid_till_date == null) {
// if user had not selected valid_till_date then set to expire after system max no of days
var updatedAt = results[i].get('updatedAt');
if (currentDate.diff(updatedAt,'days') > 10) {
console.log("Permanent change row to be set inactive. Updated at - " + currentDate.diff(updatedAt)+ updatedAt)
results[i].set('isActive',false)
isSaveNeeded = true
}
} else if (currentDate.diff(valid_till_date) > 0) {
// check whether valid_till_date has passed
console.log("Found row with elapsed valid date " + results[i].id)
results[i].set("isActive",false)
isSaveNeeded = true
}
if (isSaveNeeded == true) {
console.log("Record needs to be saved for " + results[i].id)
results[i].save(null, {useMasterKey:true})
.then(function (user) {
++noOfRowsUpdated
console.log("reset : Object ID: " + results[i].id + " saved - " + noOfRowsUpdated)
})
.catch(function (error) {
console.log("reset : Error saving Object ID: " + results[i].id + error);
response.error(error);
})
} else {
console.log("Record not to be saved for " + results[i].id)
}
isSaveNeeded = false
} // end of for loop
//BELOW IS EXECUTED BEFORE FOR LOOP COMPLETES
console.log("Updated " + noOfRowsUpdated +" price rows");
response.success("Updated " + noOfRowsUpdated +" price rows")
}) // end of .then((results)
.catch(function(error) {
response.error("Failed to fetch from price" + error );
});
});

Parse.com's save runs async, so that loop finishes before the saves happen. The solution is to reorganize the code a little bit, and wait for the saves to happen before executing the response functions.
The trick is to collect the promises returned by each save in an array and wait for the fulfillment of those promises with Promise.when() (synonym for Promise.all())
To make it clearer, factor out the "is save needed" logic, so this cloud function can be only about handling the database...
Parse.Cloud.define("reset", function(request, response) {
var Query = new Parse.Query("price");
Query.equalTo('isActive', true);
Query.find({useMasterKey:true}).then((results) => {
console.log("Found " + results.length + " price rows");
// assuming ES6 or something like underscore
let pricesToSave = results.filter(price => priceNeedsSave(price));
// here's the important part, collect the promise from each save
// proceed only after the promises have completed
let promises = pricesToSave.map(price => price.save(null, {useMasterKey:true}));
return Parse.Promise.when(promises).then(() => pricesToSave.length);
}).then(count => {
response.success("Updated " + count +" price rows");
}).catch(error => {
response.error("Failed to fetch from price" + error );
});
}
Just for completeness, below is the factored-out needsSave logic. (OP should check this over, I just copied the body of the loop)...
function priceNeedsSave(price) {
var isSaveNeeded = false;
var currentDate = moment()
var valid_till_date = price.get('valid_till_date');
if (valid_till_date == null) {
// if user had not selected valid_till_date then set to expire after system max no of days
var updatedAt = price.get('updatedAt');
if (currentDate.diff(updatedAt,'days') > 10) {
console.log("Permanent change row to be set inactive. Updated at - " + currentDate.diff(updatedAt)+ updatedAt)
price.set('isActive',false)
isSaveNeeded = true
}
} else if (currentDate.diff(valid_till_date) > 0) {
// check whether valid_till_date has passed
console.log("Found row with elapsed valid date " + price.id)
price.set("isActive",false)
isSaveNeeded = true
}
return isSaveNeeded;
}

Related

JavaScript Faster way to ask for null value

I have a question. I created the following functions:
function checkGender(canidate, callback) {
var query = "SELECT a.* FROM (SELECT Id AS GebruikerId, TIMESTAMPDIFF(year, profiel_Geboortedatum, NOW()) AS Leeftijd FROM " +
"gebruikers WHERE Id = " + canidate.MedereizigerId + ") a INNER JOIN gebruikers ON gebruikers.Id = " + canidate.GebruikerId + " WHERE a.Leeftijd >= gebruikers.medereiziger_MinLeeftijd AND " +
"a.Leeftijd <= gebruikers.medereiziger_MaxLeeftijd GROUP BY a.GebruikerId;";
FYSCloud.API.queryDatabase(query).done(function (data) {
if (data.length == 1) {
callback(data);
}
else {
callback(null);
}
}).fail(function (reason) {
console.log(reason);
callback(null);
});
}
function checkAge(canidate, callback) {
var query = "SELECT a.* FROM (SELECT Id AS GebruikerId, TIMESTAMPDIFF(year, profiel_Geboortedatum, NOW()) AS Leeftijd FROM " +
"gebruikers WHERE Id = " + canidate.MedereizigerId + ") a INNER JOIN gebruikers ON gebruikers.Id = " + canidate.GebruikerId + " WHERE a.Leeftijd >= gebruikers.medereiziger_MinLeeftijd AND " +
"a.Leeftijd <= gebruikers.medereiziger_MaxLeeftijd GROUP BY a.GebruikerId;";
FYSCloud.API.queryDatabase(query).done(function (data) {
if (data.length == 1) {
callback(data);
}
else {
callback(null);
}
}).fail(function (reason) {
console.log(reason);
callback(null);
});
}
[...]
Now the queries are working like a charm but I am using the following code to call these functions:
for(var i=0; i<data.length; i++) {
// CHECK GESLACHT
checkGender(data[i], function(genderData) {
if(genderData != null) {
// CHECK LEEFTIJD
checkAge(data[i], function(ageData) {
if(ageData != null) {
// CHECK BUDGET
checkBudget(data[i], function(budgetData) {
if(budgetData != null) {
// CHECK VAKANTIELAND
checkDestinationCountries(data[i], function(destinationCountryData) {
if(destinationCountryData != null) {
// CHECK GESPROKEN TALEN
checkSpokenLanguages(data[i], function(spokenLanguagesData) {
if(spokenLanguagesData != null) {
}
});
}
});
}
});
}
});
}
});
}
What I am doing here is waiting for the function to finish and then continue with the next one, but only if the result of the function didn't return null. Now this takes up a lot of lines and tabs, so I was wondering if there was a beter way to ask everytime for a null value?
Please let me know, just out of curiosity
You can change your function to a Promise based result, instead of calling callbacks use resolve and reject, like this:
function checkAge(canidate) {
// create a new Promise and return it
return new Promise((resolve, reject) => {
var query = "SELECT a.* FROM (SELECT Id AS GebruikerId, TIMESTAMPDIFF(year, profiel_Geboortedatum, NOW()) AS Leeftijd FROM " +
"gebruikers WHERE Id = " + canidate.MedereizigerId + ") a INNER JOIN gebruikers ON gebruikers.Id = " + canidate.GebruikerId + " WHERE a.Leeftijd >= gebruikers.medereiziger_MinLeeftijd AND " +
"a.Leeftijd <= gebruikers.medereiziger_MaxLeeftijd GROUP BY a.GebruikerId;";
FYSCloud.API.queryDatabase(query).done(function (data) {
if (data.length == 1) {
resolve(data); // when successful, resolve with data
}
else {
reject('no data found'); // any error, call reject()
}
}).fail(function (reason) {
console.log(reason);
reject(reason); // any error, call reject()
});
});
}
With this, you can use async/await feature to write a generic validation method with all others validation functions, because any call to reject will throw a exception:
async function checkData(data) {
try {
const genderData = await checkGender(data);
const ageData = await checkAge(data);
const budgetData = await checkBudget(data);
const destinationCountryData = await checkDestinationCountries(data);
} catch (e) {
// some validation failed
}
}
You can use the && operator to check for a falsy value (null, 0, false, undefined, etc.) before executing a function.
value && functionCall(value)
That might make it a bit cleaner.
checkGender(data[i], genderData => genderData &&
checkAge(data[i], ageData => ageData &&
checkBudget(data[i], budgetData => budgetData &&
checkDestinationCountries(data[i], destinationCountryData => destinationCountryData &&
checkSpokenLanguages(data[i], spokenLanguagesData => spokenLanguagesData && console.log("It Works"))))))

JavaScript Callback Inner Function not Executed

I have a client-side web-application that takes a csv-file, parses it into various data types, searches for something specific, and displays a table with the answer on the screen. The search function returning a null string. This occurs because its search parameter, returned by a callback function and put into lib, returns null.
I'm fairly certain this is a callback issue, but I've messed around with the order so much I'm not sure what goes where anymore in my html...A second set of eyes would be appreciated.
The desired series of events
fileToArray() gives us an array
search() looks in the array for its specified item and returns a csv-format string containing what it found
displayTable takes that csv-format string and outputs it to the desired location
The Code
// jQuery call to fetch the client-side csv file - this works when called by itself.
const fileToArray = () => {
console.log("fileToArray started.");
$.get({
url: CSV_LOCATION,
dataType: "text",
success: function (result) {
console.log("splitting result by newline...");
let csvLines = result.split("\n");
console.log("split successful. generating array into retval ...");
let retval = [];
for (let i = 0; i < csvLines.length; i++) {
// [0][0] is number [0][1] is class, [0][2] is unit, [0][3] is lesson
retval[i] = csvLines[i].split(",");
}
console.log("success! Returning retval.");
return retval;
// callback(result);
// return result;
},
failure: function (xhr, status, error) {
console.log("ERROR: fileToString(): " + xhr + " ||| " + status + " ||| " + error);
alert("ERROR: fileToString(): " + xhr + " ||| " + status + " ||| " + error);
}
})
};
// PRECONDITION: form is #search-params in index.js
// > lib is the result of fileToArray()
// POSTCONDITION: result is a csv-format string to be passed to displayTable() in index.js
const search = (form, callback) => {
console.log("search called...");
// vvvvv The probable root of the problem vvvvv //
let lib = callback;
console.log(lib.length + " is lib's length.");
let result = "";
console.log("search nested for loop called...");
for (let i = 0; i < lib.length; i++) {
// check class
console.log("checking class " + form.class.value + "...");
if (lib[i][1] === form.class.value) {
// check unit
console.log("checking unit " + form.unit.value + "...");
if (Number(lib[i][2]) === Number(form.unit.value)) {
console.log("adding to result...");
result += lib[i] + "\n";
}
}
}
console.log("search success! result: " + result.length + " characters");
console.log(result);
return result;
};
<!-- I'm almost 100% certain I've messed up the callback in this button,
but I still don't quite understand how... I've played with
displayTable(fileToArray(search(...))), but I don't quite know how it should go -->
<button class="btn btn-primary"
onclick="displayTable(search(document.getElementById('search-params'), fileToArray), $('#card-display'))">
Submit
</button>
What I've tried
I have looked to the following sites for inspiration (none have helped):
JavaScript is Sexy
JavaScript: Passing parameters to a callback function
JavaScript Callback Functions
Passing arguments to callback functions
In Summary
It's painfully obvious I still don't understand callbacks fully. Any help would be appreciated.
You could use async / await
const displayTable = async () => {
let arrayFromFile = await fileToArray(); // fileToArray executes and assigns the returned value when it completes
let searchedData = search(form, arrayFromFile);
// Display the table
};
Thanks to #kapantzak for the inspiration!! Turns out, I was using callbacks horribly bass-ackwards. According to this, the old-school async style is something akin to
doSomething(function(result) {
doSomethingElse(result, function(newResult) {
doThirdThing(newResult, function(finalResult) {
console.log('Got the final result: ' + finalResult);
}, failureCallback);
}, failureCallback);
}, failureCallback);
So, the relevant code now looks like this:
const fileToArray = (callback) => {
// console.log("fileToArray started.");
$.get({
url: CSV_LOCATION,
dataType: "text",
success: function (result) {
let csvLines = result.split("\n");
let retVal = [];
for (let i = 0; i < csvLines.length; i++) {
// [0][0] is number [0][1] is class, [0][2] is unit, [0][3] is lesson
retVal[i] = csvLines[i].split(",");
}
callback(retVal);
},
failure: function (xhr, status, error) {
console.log("ERROR: fileToString(): " + xhr + " ||| " + status + " ||| " + error);
alert("ERROR: fileToString(): " + xhr + " ||| " + status + " ||| " + error);
}
})
};
// =======
const search = (form, lib, callback) => {
let result = "";
let formClass = form.class.value.toLowerCase();
let formUnit = form.unit.value.toLowerCase();
let formLesson = form.lesson.value.toLowerCase();
for (let i = 0; i < lib.length; i++) {
// check class
if (lib[i][1].toLowerCase() === formClass) {
// check unit
if (Number(lib[i][2].toLowerCase()) === Number(formUnit)) {
result += lib[i] + "\n";
}
}
}
console.log(result);
callback(result);
};
<button class="btn btn-primary"
onclick="fileToArray(function(result) {
search(document.getElementById('search-params'), result, function(newResult) {
displayTable(newResult, $('#card-display'));
});
});">
Submit
</button>
This righted the wrongs and caused my search and display to function properly.

Possible to update the field in below datastructure in firestore database using cloud funct

I have a datastructure in the form of object structure . [![enter image description here][1]][1]
{
Invite1
{
Amount: 10,
PhoneNumber:9876543210,
Status:"Pending"
}
Invite2
{
Amount: 20,
PhoneNumber:1234566789,
Status:"Pending"
}
}
I have a condition when whose Invite(1,2,3) PhoneNumber matches with other document that invitee need to update the field as Status = true
When I try to update a field as Status = true It is updating at the end of the document.
Mycode need to update
var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph + "" + status + " to pay $" + document.Amount;
var fulldoc = dbref.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document');
} else {
console.log('Document data :', doc.data());
d1 = doc.data();
console.log("d1 is" + d1);
for (var k in d1) {
var p = d1[k].PhoneNumber;
console.log("ivitees phone" + p);
if (receiverph == p) // Here the condition is true of the invite phoneNumber then need to update
{
console.log("p" + PhoneNumber);
console.log("the phonenumber matches");
var updated = dbref.update({"Status":status});// Here It is updating
at the endof the document
other Method to update
d1.Status = status; // In d1 I have the document data
var setdata = dbref.set(d1);
Please if their is any approach help with me.
Thanks
If I understand correctly that you would like to update each InviteXX item in the document, here is a code that will work (I've kept the main part of your code):
var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph +"" + status +" to pay $"+document.Amount;
const fulldoc = dbref.get()
.then(doc => {
if (doc.exists) {
//console.log("Document data:", doc.data());
const inviteUpdate = {}; //An object that we will update by looping over the InviteXX objects of the document
const d1 = doc.data();
//console.log("d1 is" + d1);
for (let key in d1) {
if (d1.hasOwnProperty(key)) {
const p = d1[key].PhoneNumber;
//console.log(key);
//console.log("invitees phone " + p);
if (receiverph === p) // Here the condition is true of the invite phoneNumber then need to update
{
inviteUpdate[key + '.status'] = true;
//The key point is here: we define the object field with a mix of dot notation and []
}
}
}
return dbref.update(inviteUpdate);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
throw "No such document"
}
})
.catch(function (error) {
console.log("Error:", error);
});
Note that:
You should return a promise in your Cloud Function. I don't have the full code of your function but probably you will return fulldoc. (You may have a look at this video https://www.youtube.com/watch?v=652XeeKNHSk&t=2s)
I don't know how do you get/initialize receiverph
You may use const or let instead of var in a Cloud Function (JavaScript ES6+)

How to compare two arrays in JavaScript even if they aren't ordered?

This question has been asked so many times but I can't make what I want so I ask for your help.
I have 2 arrays checkMyDataSources and lesInfosMachines.
I need to run through checkMyDataSources to check if there is no occurrence of any items in lesInfosMachines.
The content of checkMyDataSources can be something like ["datasource_A","datasource_B","datasource_D","datasource_C"] and the name is linked with the name of each item in lesInfosMachines who contains thing like ["A","B","C","D"].
The problem is that I'm not able to run through all the checkMyDataSources, I mean when the cell A and Amachine are different it call createDataSourcedespite Amachine is maybe in the cell D.
var lesInfosMachines = InfosMachines.find({});
if(checkMyDataSources.length < 1){
console.log("there is not datasource, we will create them all");
callInitDS();
}else{
console.log("there is datasource, we will check them");
lesInfosMachines.forEach(Meteor.bindEnvironment(function(machineInfo) {
console.log("test machine " + machineInfo.nameMachine)
for (var i = 0; i < checkMyDataSources.length; i++) {
console.log("test on " + checkMyDataSources[i].name.split("_")[1]);
if(checkMyDataSources[i].name.split("_")[1] === machineInfo.nameMachine){
console.log("Datasource: " + machineInfo.nameMachine + " already exist." );
}else{
if(machineInfo.ipAddr != null){
console.log("going to create " + machineInfo.nameMachine);
createDataSource(machineInfo.nameMachine, machineInfo.ipAddr);
}else{
console.log("going to create " + machineInfo.nameMachine +
" with a fake #ip because it was null
ONLY FOR TESTING WE NEED TO REMOVE THIS"
);
createDataSource(machineInfo.nameMachine, "myFakeIP");
}
};
}
}));
console.log("test finished")
}
I hope my question is understandable and thank you for the help
[EDIT] that's my output :
[EDIT2] to simplify I want to test aMachine on A,B,C,D of checkMyDataSources and if there isn't aMachine in one of those cells (but at the end) then call createDataSource()
have you heard about lodash ?
const _ = require('lodash');
let checkMyDataSources = ["datasource_A","datasource_B","datasource_D","datasource_C"];
let lesInfosMachines = ["A","B","C"];
_.difference(checkMyDataSources, _.map(lesInfosMachines, (elt) => 'datasource_' + elt));
>[ 'datasource_D' ]
const datasources = ["datasource_A", "datasource_B", "datasource_D", "datasource_C"];
const lesInfosMachines = ["A", "D", "C"];
const prefixLength = "datasource_".length
If you want to get datasourceswhich are not on lesInfosMachines:
datasources.filter((d) => lesInfosMachines.every((l) => l !== d.slice(prefixLength)))
["datasource_B"]
If you want to get datasourceswhich are on lesInfosMachines:
datasources.filter((d) => lesInfosMachines.some((l) => l === d.slice(prefixLength)))
["datasource_A", "datasource_D", "datasource_C"]
If you want it to return trueif there are some of the datasourcesin lesInfosMachines, and false otherwise:
let otherLesInfosMachines = ["X", "Y", "Z"]
datasources.some((d) => otherLesInfosMachines.some((l) => l === d.slice(prefixLength)))
false
datasources.some((d) => lesInfosMachines.some((l) => l === d.slice(prefixLength)))
true
By combining functions such as filter, every, and some, you can implement many algorithms very idiomatically, without having rely on difficult to understand, and even semantically meaningless, forloops and indexes.
Instead of connecting the two loops, leading to multiple comparsions of the same element, you could use Array.prototype.includes()
This would make your code approximately look like this:
var lesInfosMachines = InfosMachines.find({});
if (checkMyDataSources.length < 1) {
console.log("there is not datasource, we will create them all");
callInitDS();
} else {
console.log("there is datasource, we will check them");
lesInfosMachines.forEach(Meteor.bindEnvironment(function(machineInfo) {
console.log("test machine " + machineInfo.nameMachine);
if (checkMyDataSources.includes("datasource_" + machineInfo.nameMachine) {
console.log("Datasource: " + machineInfo.nameMachine + " already exist.");
} else {
if (machineInfo.ipAddr != null) {
console.log("going to create " + machineInfo.nameMachine);
createDataSource(machineInfo.nameMachine, machineInfo.ipAddr);
} else {
console.log("going to create " + machineInfo.nameMachine + " with a fake #ip because it was null ONLY FOR TESTING WE NEED TO REMOVE THIS");
createDataSource(machineInfo.nameMachine, "myFakeIP");
}
};
}
}));
console.log("test finished")
}
Otherwise you would have to reorganize your loops as in Jeromes answer.
I have found a solution by using a boolean like this:
var hasTestedAll = false;
var lesInfosMachines = InfosMachines.find({});
if(checkMyDataSources.length < 1){
console.log("there is not datasource, we will create them all");
callInitDS();
}else{
console.log("there is datasource, we will check them");
lesInfosMachines.forEach(Meteor.bindEnvironment(function(machineInfo) {
console.log("test machine " + machineInfo.nameMachine)
for (var i = 0; i < checkMyDataSources.length; i++) {
console.log("test on " + checkMyDataSources[i].name.split("_")[1]);
if(checkMyDataSources[i].name.split("_")[1] === machineInfo.nameMachine){
if(i == checkMyDataSources.length-1){
hasTestedAll = true;
}
console.log("Datasource: " + machineInfo.nameMachine + " already exist." );
}else if(hasTestedAll){
if(machineInfo.ipAddr != null){
console.log("going to create " + machineInfo.nameMachine);
createDataSource(machineInfo.nameMachine, machineInfo.ipAddr);
}else{
console.log("going to create " + machineInfo.nameMachine + " with a fake #ip because it was null ONLY FOR TESTING WE NEED TO REMOVE THIS");
createDataSource(machineInfo.nameMachine, "myFakeIP");
}
};
}
}));
console.log("test finished")
}
(sorry for the CodeSnipet that doesn't work but I wasn't able to paste the code with a correct alignment)
So it test if I'm in the last cell of checkMyDataSources and if it's the case I allow to call the create method

How to use Promise to return value from assync Angular function into calling method?

I have list which is filled in for cycle where I calling assync function by thos way:
In For cycle I'm calling
row.SUCCES_RATE_SINCE = $scope.computeSuccessRateSinceStart(row);
Called function
// Calculate percentage of a whole from since
$scope.computeSuccessRateSinceStart = function(row) {
db = window.sqlitePlugin.openDatabase({name:"callplanner"});
// GET APPT COUNT
db.transaction(function(tx) {
tx.executeSql(sqlQuery, [], function(tx,results){
// init empty array for results
for (var i=0; i < results.rows.length; i++){
row = results.rows.item(i);
//Udpate date for writeout
//row.DATE = moment(row.DATE).format('ddd DD.M');
console.log("row APPT count is " + JSON.stringify(row));
apptCnt = row.APPT_CNT;
convCnt = row.CONVERS_CNT;
dailySuccessRateSince = apptCnt / convCnt * 100;
console.log("Success rate since is " +dailySuccessRateSince);
// THIS IS NOT WORKING
return Math.round(dailySuccessRateSince);
}
});
},function (e) {
console.log("ERROR: " + e.message);
$ionicLoading.show({
template: $translate.instant('ERROR_DATABASE'),
duration:1000
});
});
};
Problem is that computed value is always returned null (return function is executed before value is available in scope).
I'm quite new in Angular but i found that this issue could be solved using promises. Could somebody give me the example how to return value properly?
Many thanks for any help.
EDIT:
Called method is now triggered, but i cannot pass returned value into variable like this:
var test = $scope.computeSuccessRateSinceStart(row).then(function(result){
//ALERT WITH VALUE WORKS FINE
alert("Result " + result);
return result;
});
// THIS GIVES ME EMPTY ARRAY {}
alert("Result " + JSON.stringify(test));
Why don't you just make your method such that it always returns a promise, and then extract the result from the promise?
$scope.computeSuccessRateSinceStart = function(row) {
var deferred = $q.defer();
db = window.sqlitePlugin.openDatabase({name:"callplanner"});
// GET APPT COUNT
db.transaction(function(tx) {
tx.executeSql(sqlQuery, [], function(tx,results){
// init empty array for results
for (var i=0; i < results.rows.length; i++){
row = results.rows.item(i);
//Udpate date for writeout
//row.DATE = moment(row.DATE).format('ddd DD.M');
console.log("row APPT count is " + JSON.stringify(row));
apptCnt = row.APPT_CNT;
convCnt = row.CONVERS_CNT;
dailySuccessRateSince = apptCnt / convCnt * 100;
console.log("Success rate since is " +dailySuccessRateSince);
// THIS IS NOW WORKING:
deferred.resolve(Math.round(dailySuccessRateSince));
}
});
}, function(e) {
console.log("ERROR: " + e.message);
deferred.reject(e);
});
return deferred.promise;
};
Usage:
$scope.computeSuccessRateSinceStart(row).then(function(result){
// THIS GIVES THE VALUE:
alert("Result " + JSON.stringify(test));
return result;
}, function(e)
$ionicLoading.show({
template: $translate.instant('ERROR_DATABASE'),
duration:1000
});
});

Categories