1

I am new to Node.JS coming from a Java Background I am using express to build this Rest API . What I am trying to do is build the concept of a manager. I am looking for a elegant way of returning a user object in the following:

users route: user.js

router.get('/find/:email', function(req, res, next){
  userWare.findUserByEmail(req, res, next)
});

middleware/manager: usermiddleware.js

module.exports = {
    findUserByEmail: function(req, res, next) {
        models.core_user.find({
            where:{
                email: req.params.email
            }
        }).then(function(user){
            res.json(user)
        }, function(err){
            res.status(404).json(err);
        });
    },
}

So In this above function I would like to return the user object to the route instead of the json. so that I can create the json from the object in the route. The whole point of this manager class will be to fectch and return objects.

2
  • Return the user object to who? You can't pass native JS objects to other processes or as the result of an HTTP call. You can pick certain attributes out of that object and return just those as JSON (which is a text format), but you can't return a JS object to some other process or as the result of an HTTP call. That JS object belongs only to this instance of the Javascript V8 engine running in node.js and only works in that context. You can't take it outside that V8 environment. Commented Mar 12, 2016 at 9:54
  • If your userWare database has a unique ID for each user, perhaps you just need to return the ID as JSON or as a string and then the caller of the API can use that ID in subsequent API calls to identify that particular user. This is usually how problems like this are solved. An object in the database is reduced to some sort of unique key and that key is what is exchanged between processes and if you then need to get back to that specific user object on some subsquent API call, you can use the key to quickly get the corresponding user object. Commented Mar 12, 2016 at 9:58

1 Answer 1

3

What you need to do is call the callback function with the data you need or return the promise.

Callback

user.js

router.get('/find/:email', function (req, res, next) {
    userWare.findUserByEmail(req.params.email, function (err, data) {
        // error as first parameter or null if no error occurred
        if (err) {
            return res.status(404).json(err);
        }
        res.json(user);
    });
});

usermiddleware.js

module.exports = {
    findUserByEmail: function (email, next) {
        models.core_user.find({
            where: {
                email: email
            }
        }).then(
            function (user) {
                // async call of callback with user object
                next(null, user);
            }, 
            function (err) {
                // async call of callback with error
                next(err);
            }
        );
    }
};

Promise

You could also just return the promise returned by your model, then it would look like this:

user.js

router.get('/find/:email', function (req, res, next) {
    userWare.findUserByEmail(req.params.email).then(
        function (user) {
            res.json(user);
        }, 
        function (err) {
            res.status(404).json(err)
        }
    );
});

usermiddleware.js

module.exports = {
    findUserByEmail: function (email) {
        // return the promise to the caller
        return models.core_user.find({
            where: {
                email: email
            }
        });
    }
};
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.