Domanda

So, I am using mean.io and for some reason, my routes.js never hits my 'index.all' method, or the 'exports.all' function, even though I require the functions from the server-side controller. Also, my routing is done using angular-ui-router. Does anybody know how to call a backend method from routing in MEAN.IO? I keep using:

 'use strict';

    module.exports = function(System, app, auth, database) {

        // Home route
        var index = require('../controllers/index');
 app.route('/test').get(index.all);
        app.route('/')
            .get(index.render);

    };

I would like to hit 'index.all' but even if I navigate to /test, it still gets index.render. Does anybody know why?

Here is the controllers file:

 'use strict';

    var mean = require('meanio');
    var mongoose = require('mongoose');
    var Composition = mongoose.model('Composition');

    exports.render = function(req, res) {
    console.log(req.user);
        var modules = [];
        // Preparing angular modules list with dependencies
        for (var name in mean.modules) {
            modules.push({
                name: name,
                module: 'mean.' + name,
                angularDependencies: mean.modules[name].angularDependencies
            });
        }

        function isAdmin() {
            return req.user && req.user.roles.indexOf('admin') !== -1;
        }


        // Send some basic starting info to the view    
        res.render('index', {
            user: req.user ? {
                name: req.user.name,
                _id: req.user._id,
                username: req.user.username,
                roles: req.user.roles
            } : {},
            modules: modules,
            isAdmin: isAdmin,
            adminEnabled: isAdmin() && mean.moduleEnabled('mean-admin')
        });
    };

    exports.all = function(req, res) {

        console.log(req.user);
        Composition.find({user: req.user}, 'title description').sort('-created').populate('user', 'name username').exec(function(err, compositions) {
            if (err) {
                return res.jsonp(500, {
                    error: 'Cannot list the compositions'
                });
            }
            res.jsonp(compositions);

        });
    };

Is this a front-end or backend problem? Thanks for any advice that might be helpful.

È stato utile?

Soluzione

You are navigating. So are you hitting the link in the browser url? Then you should try localhost:3000/test instead of localhost:3000/#!/test.

The urls of the form localhost:3000:/#!/<something> are angular routes. Look up angular routing and views. It is better to use angular views than server side rendering. Do angular routing for test and add a view corresponding to it. Fetch the dynamic data in the view using the regular $http.get calls.

Check this tutorial for routing and adding views in angular

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top