Friday, 9 June 2017

Need to pass value between functions but get "TypeError: callback is not a function"

I know this error isnt new... I'm trying to return changes values to another function when i call it and deal with the value.

Thhe uciGetChanges returns a json like this {"jsonrpc":"2.0","id":1,"result":[0,{"changes":[["set","mwan1","enabled","1"]]}]} where i just need to look to changes array if it has any value or not.

My function:

    UbusController.prototype.uciGetChanges = function (procedure, signature, device) {

        var changes;
        Controllers.Ubus.getSession(device.id).then(function (dataAuth) {
            if (dataAuth) {
                Controllers.Ubus.execCommand(device.id, "uci", procedure, signature).then(function (data, callback) {
                    var res=data;
                    if (data.result) {
                        if (data.result[0] == 0) {
                            if(data.result[1]["changes"]!=[]){
                                changes=data.result[1]["changes"];
                                return callback(changes);
                            }
                        }
                    } else {
                        changes=[];
                        return callback(changes);
                    }
                });
            } else {
                throw "no data auth";
            }
        }).catch(function (e) {
            throw e;
        });
    }

and here is where i need to use that value,

Controllers.Ubus.uciGetChanges('changes', {"config": "teste_network"}, device[0]).then(function(changesData){  
                if (changesData.result[0] == 0){
                    if(changesData.result[1].changes!=[]){
                        res.status(404).end();
                    }
                }
        }).catch(function (e) {
            res.status(500).send(e);
        });



via Caty Matos

No comments:

Post a Comment