Trewaters
Trewaters

Reputation: 1079

How to retrieve data from api res.send?

I am using the javascript MEAN stack for my single page app.

I have an Angular factory making a call to my Api.

app.factory('authorizing', function($resource){
    return $resource(
        '/api/authorizing/:username',
        {'username':'@username'},           
    // retreiving only one user and all their roles
        {'singleUser' : {
            method:'GET'
            ,isArray: false
            }}
    );
});

I call the factory in my controller like this. My goal is to update the web page data based on the response I get back from the Api. I expect a true or false value to be returned. I have tried other things but I want to keep my authorization on the server side.

app.controller('usrPageController', function ($scope, usrServiceById, $route, authorizing, $rootScope) {

    $scope.updateUser = function (data,field){

        var vCheckUserRoles;
        vCheckUserRoles = authorizing.singleUser({'username': $rootScope.current_user});

        if (vCheckUserRoles == true){
            usrServiceById.update({'username':username},{field:data, 'fieldName':field});
        };
    };
    });

The database returns the result data using a res.send.

.get(function (req, res) {
        RoleUser.find({ 'aclUserName': req.params.username }, function (err, aclUser) {
            if (err) { return res.sendStatus(500) };

            // return an error if no user is found
            if (aclUser == null) { return res.sendStatus(401) };

            //check for proper role in list of users
            for (var i = 0; i < aclUser.length; i++) {
                if (aclUser[i].aclUserResource == req.params.username + '_PROFILE') {

                    //return a successful status
                    return res.send('true');

                };
            };

            //return a failed status
            return res.send('false');
        });
    });

I don't get any errors from the code but the return object is empty when it hits the controller after the res.send. I have tried different types of data to return but nothing seems to work for me. Any help is appreciated. I have othe res.send calls in my Api. They work but I take the data directly from my database wiht the callback and feed it to the res.send. This is the only time in my code that I am trying to return something besides the successful callback variable.

UPDATED CODE:

I removed the var vCheckUserRoles. Now the value is passed to a success callback

app.controller('usrPageController', function ($scope, usrServiceById, $route, authorizing, $rootScope) {

        authorizing.singleUser({'username': $rootScope.current_user},function(response){
            console.log('response = ' + response.data)
                if (response.data == 'true'){
            usrServiceById.update({'username':usrSingle.username},{field:data, 'fieldName':field});
        };
        });
    });

Upvotes: 2

Views: 6629

Answers (2)

user6184932
user6184932

Reputation:

On the server you send data as follows:

res.status(200).send({message: "Hello!"});

In the front-end you receive the data, then resolve it to get data as follows:

fetch(url)
.then(response => {
    if(response.ok) {
        return response.json();
    }
}).then(data => {
    if(data) {
        console.log(data);
    }
}).catch(err => console.error(err));

Upvotes: 2

KCFragrance
KCFragrance

Reputation: 158

You can use res.json instead of res.send to send status of your query.

Example //// res.json({status:true})

And on client side you can access that status value in data.status field.

Upvotes: 3

Related Questions