0

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);

1 Answer 1

0

Your callback is node callback style. Angular does not use this type of callback. The returned value contains data field where the actual response is stored.

This assumes you use $http

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.