The createQuestionDataAsync is not returning data. The flQuestion.create function makes a call to an express service that writes to a mongodb collection. The question is written and the value comes back and stored in "data". I could not set this up like a typical callback (err,data) as the response back only return one value. Maybe I need to make this a promise? Or can I keep a callback with some modification?
angular.module('app').factory('flQuestionCrud',function($http, $q, $state, flQuestion ){ return { createNewQuestion: function(newQuestionData) { console.log("Before - flQuestion.create");
// Question Call Back Function
function questionCallBack(err, data){
if (err){
console.log("flQuestionCrud - Error " + err);
console.log("flQuestionCrud - Error Statement " + err);
return null;
//$state.go('questionCreate');
}else {
console.log("flQuestionCrud - Success " + data);
console.log("flQuestionCrud - Success Statement " + data.statement);
console.log("flQuestionCrud - Success Question id " + data._id);
return data;
//$state.go('questionUpdate');
}
}
// Try/catch
function createQuestionDataAsync(questionData, cb) {
console.log("flQuestionCrud - Before Call to create ")
flQuestion.create(questionData, function(data) {
console.log("flQuestionCrud - After Call to create ")
if (!data){
return cb("Error Creating Data");
//return null;
}
try {
console.log("flQuestionCrud - Try Section - Success " + data);
console.log("flQuestionCrud - Try Section - Success Statement " + data.statement);
console.log("flQuestionCrud - Try Section - Success Question id " + data._id);
}
catch (err) {
return cb("Error Creating Data");
//return null;
}
return cb(null,data);
//return data;
});
}
createQuestionDataAsync(newQuestionData,questionCallBack);