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.
userobject 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.userWaredatabase 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.