i'm trying to build a simple app using meanjs. i have 2 modules: the standard user module and a posts module. what i want to achieve is a user profile page which will display some info about particular user and list posts belong to that user. you can think of it as a twitter profile page. i thought it would also be best /users/username shaped url structure. but i'm fairly new on all this and now stuck. here is what i did so far:
i added these lines into users.server.routes.js:
app.route('/api/users/:userName').get(users.userByUsername);
app.param('userName', users.userByUsername);
added a function to server control like this:
exports.userByUsername = function(req, res, next, username) {
User.findOne({
username: username
}).exec(function(err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + username));
req.profile = user;
next();
});
};
i added this part to users.client.routes.js
state('view.profile', {
url: '/users/:username',
templateUrl: 'modules/users/views/view-profile.client.view.html'
});
i created a new view like this:
<section class="row" data-ng-controller="ViewProfileController" ng-init="findUser()">
<div class="col-xs-offset-1 col-xs-10 col-md-offset-4 col-md-4">
...
</div>
</section>
and finally i created a new client controller:
angular.module('users').controller('ViewProfileController', ['$scope', '$http', '$stateParams', '$location', 'Users', 'Authentication',
function($scope, $http, $location, Users, Authentication, $stateParams) {
$scope.user = Authentication.user;
$scope.findUser = function () {
$scope.ourUser = Users.get({
username: $stateParams.username
});
};
}
]);
when i try to open users/username page for any username, it just gives the main homepage to me. any ideas what is wrong or what is missing? thanks in advance.