I'm trying to add a new controller (compareGroup) in my MEAN stack. In my route, it says compareGroup is not defined.

StackOverflow https://stackoverflow.com/questions/22658952

Question

Below are what I believe are the relevant files:

compareGroup model

 'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;


/**
 * CompareGroup Schema
 */
var CompareGroupSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    optionName: {
        type: String,
        default: '',
        trim: true
    },
    optionAttributes: [{
        attributeName: String,
        optionImportance: Number,
        optionScore: Number,
        optionuom: String
    }]
});

mongoose.model('CompareGroup', CompareGroupSchema);

compareGroup route

'use strict';

var index = require('../controllers/compareGroups');

module.exports = function(app) {
    app.post('/compareGroups', compareGroups.create);
};

backend controller

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    CompareGroup = mongoose.model('CompareGroup'),
    _ = require('lodash');

exports.create = function(req, res) {
    var CompareGroup = new CompareGroup(req.body);

    compareGroup.save(function(err) {
        if (err) {
            return res.send('users/signup', {
                errors: err.errors,
                compareGroup: compareGroup
            });
        } else {
            res.jsonp(compareGroup);
        }
    });
};

And my front end controller

'use strict';

angular.module('mean.compareGroups').controller('CompareGroupsController', ['$scope', '$stateParams', '$location', 'Global', 'CompareGroups', function ($scope, $stateParams, $location, Global, CompareGroups) {
    $scope.global = Global;

    $scope.create = function() {
        var compareGroup = new CompareGroups({
            //"this" is the same thing as scope
            optionName: this.optionName,
            optionAttributes: this.optionName.optionAttributes,
            attributeName: this.optionAttributes.attributeName,
            optionImportance: this.optionAttributes.optionImportance,
            optionScore: this.optionAttributes.optionScore,
            optionuom: this.optionAttributes.optionuom
        });

        compareGroup.$save(function(response) {
            $location.path('compareGroups/');
        });
    };
}]);

When I try to run this code, my terminal says: ".../Mean/app/routes/compareGroups app.post('/compareGroups', compareGroups.create);

                       ^

ReferenceError: compareGroups is not defined."

Let me know what other information you need. Thanks!

Was it helpful?

Solution

In your route file change this:

var index = require('../controllers/compareGroups');

to this:

var compareGroups = require('../controllers/compareGroups');

then you'll be able to call the controller properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top