您好,
我正在使用MEAN堆栈编写TODO应用程序。 我使用http://www.ibm.com/developerworks/library/wa-nodejs-polling-app/的投票应用作为我的基础。
我正在尝试实现两个基本操作:获取所有待办事项并删除待办事项。
我已经成功完成了在MongoDB中存储数据的部分。我也成功测试了我与数据库的连接。
但是,当我运行我的代码时,我收到以下错误:
Error: .get() requires callback functions but got a [object Undefined]
at C:\basestation\utopianSpace\ToDo\node_modules\express\lib\router\index.js:252:11
at Array.forEach (native)
at Router.route (C:\basestation\utopianSpace\ToDo\node_modules\express\lib\router\index.js:248:13)
at Router.(anonymous function) [as get] (C:\basestation\utopianSpace\ToDo\node_modules\express\lib\router\index.js:270:16)
at Function.app.(anonymous function) [as get] (C:\basestation\utopianSpace\ToDo\node_modules\express\lib\application.js:414:26)
at Object.<anonymous> (C:\basestation\utopianSpace\ToDo\app.js:31:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
我的app.js
(function() {
var app = angular.module('todo', [ 'ngRoute', 'todoServices' ]);
app.controller('ToDoController', function($scope) {
this.createToDo = function(todo) {
$scope.todos.push(todo);
$scope.todo = {};
}
this.removeToDo = function(todo, index) {
$scope.todos.splice(index, 1);
}
});
app.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/todo', {
templateUrl : 'partials/list.html',
controller : TodoListCtrl
});
} ]);
function TodoListCtrl($scope) {
$scope.todos = tasks;
}
var tasks = [
{
tname : "Buy Milk",
tdate : new Date(),
tstatus : "Hold"
}, {
tname : "Pay bill",
tdate : new Date(),
tstatus : "Completed"
}, {
tname : "Go fishing",
tdate : new Date(),
tstatus : "Started"
}, {
tname : "Watch movie",
tdate : new Date(),
tstatus : "Pending"
} ];
})();
index.js
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost/', 'tododb');
db.on('error', function () {
console.log('Error! Database connection failed.');
});
db.once('open', function (argument) {
console.log('Database connection established!');
});
var ToDoSchema = require('./../models/todo.js').todoSchema;
var Todo = mongoose.model('todoSchema', ToDoSchema);
exports.index = function(req, res) {
res.render('index', {
title : 'Todo'
});
};
todo.js
var mongoose = require('mongoose');
var todoSchema_ = new mongoose.Schema({
tname : 'String',
tdate : 'String',
tstatus : 'String'
});
exports.todoSchema = todoSchema_;
node - app.js
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/todo', routes.todo);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
services.js
angular.module('todoServices', [ 'ngResource' ]).factory('ToDO',
function($resource) {
return $resource('todo', {}, {
query : {
method : 'GET',
isArray : true
}
})
})
routes.js
/**
* Module dependencies.
*/
var utils = require('../utils');
/**
* Expose `Route`.
*/
module.exports = Route;
/**
* Initialize `Route` with the given HTTP `method`, `path`,
* and an array of `callbacks` and `options`.
*
* Options:
*
* - `sensitive` enable case-sensitive routes
* - `strict` enable strict matching for trailing slashes
*
* @param {String} method
* @param {String} path
* @param {Array} callbacks
* @param {Object} options.
* @api private
*/
function Route(method, path, callbacks, options) {
options = options || {};
this.path = path;
this.method = method;
this.callbacks = callbacks;
this.regexp = utils.pathRegexp(path
, this.keys = []
, options.sensitive
, options.strict);
}
/**
* Check if this route matches `path`, if so
* populate `.params`.
*
* @param {String} path
* @return {Boolean}
* @api private
*/
Route.prototype.match = function(path){
var keys = this.keys
, params = this.params = []
, m = this.regexp.exec(path);
if (!m) return false;
for (var i = 1, len = m.length; i < len; ++i) {
var key = keys[i - 1];
var val = 'string' == typeof m[i]
? decodeURIComponent(m[i])
: m[i];
if (key) {
params[key.name] = val;
} else {
params.push(val);
}
}
return true;
};
道歉,因为你可能会发现多个错误。
问候
ABHI