Search code examples
angularjsmongooseangularjs-service

CastError: Cast to ObjectId failed for value "users.client.module.js" at path "_id"


This is again another error I'm getting following through a MEAN stack development book. We've just added a new users module and a users service for allowing angular to use user information from an authenticated user. I'm getting a 500 error in the terminal for the following file

'/users/users.client.module.js'

This is the associated services file that is going with it. From what I've read this is a Mongoose error. Why is this popping up? I haven't even authenticated a user yet, this happens on page load for index.

Index Controller

exports.render = function(req,res) {

res.render('index', {
    title: 'Hello World',
    user: JSON.stringify(req.user)
});

};

Index.ejs

<!DOCTYPE html>

<html xmlns:ng="http://angularjs.org">
<head>
    <title><%= title%></title>
</head>
<body>
    <% if (user) { %>
        <a href='/signout'>Sign Out</a>
    <% } else { %>
            <a href='/signup'>Sign-up</a>
            <a href='/signin'>Sign-in</a>
    <% } %>

    <h1><%= title%></h1>

    <section ng-view></section>

    <script type="text/javascript">
        window.user = <%- user || 'null' %>
    </script>

    <script type="text/javascript" src="/lib/angular/angular.js"></script>
    <script type="text/javascript" src="/lib/angular-route/angular-route.js"></script>
    <script type="text/javascript" src="/users/users.client.module.js"></script>
    <script type="text/javascript" src="/users/services/authentication.client.service.js"></script>
    <script type="text/javascript" src="/application.js"></script>
    <script type="text/javascript" src="/example/example.client.module.js"></script>
    <script type="text/javascript" src="/example/controllers/example.client.controller.js"></script>
    <script type="text/javascript" src="/example/config/example.client.routes.js"></script>

</body>

Users Module

angular.module('users',[]);

Users Authentication Service

angular.module('users').factory('Authentication', [
function(){
    this.user = window.user;

    return {
        user: this.user
    };
}
]);

Edit - 2/2/2016 Re-ordered script tags in the view to this order, but still a no go.

    <script type="text/javascript" src="/lib/angular/angular.js"></script>
    <script type="text/javascript" src="/lib/angular-route/angular-route.js"></script>


    <script type="text/javascript" src="/example/example.client.module.js"></script>
    <script type="text/javascript" src="/example/controllers/example.client.controller.js"></script>
    <script type="text/javascript" src="/example/config/example.client.routes.js"></script>

    <script type="text/javascript" src="/users/users.client.module.js"></script>
    <script type="text/javascript" src="/users/services/authentication.client.service.js"></script>

    <script type="text/javascript" src="/application.js"></script>

Solution

  • The source code for the book leads to an error when using the users.client.module.js module. In my analysis, the static file /users/users.client.module.js loaded in index.ejs seemed to be interpreted as a server route /users/:userId leading to search a user in MongoDB with ID user.client.module.js. The issue has been resolved by placing the app.use(express.static('./public') statement at the beginning of setup (after the statement var app = express(); for example) in the express.js configuration file but sure before de require routes statements.

    NOTE: The author added this line in the book's github repo (just before the closing, "return app"):