Route.get()需要回调函数,但会收到"object undefined"(对象未定义) [英] Route.get() requires callback functions but got a "object Undefined"
问题描述
我正在学习制作Todo应用程序. 在网站上,我正在研究的是 https://coderwall.com/p/4gzjqw/build-a-javascript-todo-app-with-express-jade-and-mongodb
I am learning making Todo app. On the website, I am studying is https://coderwall.com/p/4gzjqw/build-a-javascript-todo-app-with-express-jade-and-mongodb
我按说明输入了
[app.js]
var main = require('./routes/main');
var todo = require('./routes/todo');
var todoRouter = express.Router();
app.use('/todos', todoRouter);
app.get('/', main.index);
todoRouter.get('/',todo.all);
todoRouter.post('/create', todo.create);
todoRouter.post('/destroy/:id', todo.destroy);
todoRouter.post('/edit/:id', todo.edit);
[/routes/todo.js]
module.exports ={
all: function(req, res){
res.send('All todos');
},
viewOne: function(req, res){
console.log('Viewing '+req.params.id);
},
create: function(req, res){
console.log('Todo created');
},
destroy: function(req, res){
console.log('Todo deleted');
},
edit: function(req, res){
console.log('Todo '+req.params.id+' updated');
}
};
我收到此错误消息
错误:Route.get()需要回调函数,但得到了[对象 未定义]
Error: Route.get() requires callback functions but got a [object Undefined]
我在这里错过了什么吗?
Did I miss something here?
推荐答案
在本教程中,todo.all
返回一个callback
对象.这是router.get
语法所必需的.
In the tutorial the todo.all
returns a callback
object. This is required for the router.get
syntax.
从文档中:
router.METHOD(路径,[回调,...]回调)
router.METHOD()方法提供了以下功能 Express,其中METHOD是HTTP方法之一,例如GET,PUT, POST,等等,以小写形式.因此,实际方法是 router.get(),router.post(),router.put()等.
The router.METHOD() methods provide the routing functionality in Express, where METHOD is one of the HTTP methods, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are router.get(), router.post(), router.put(), and so on.
您仍然需要在todo
文件中定义callback
对象的数组,以便可以为router
访问正确的callback
对象.
You still need to define the array of callback
objects in your todo
files so you can access the proper callback
object for your router
.
从您的教程中可以看到todo.js
包含callback
对象的数组(这是编写todo.all
时要访问的对象):
You can see from your tutorial that todo.js
contains the array of callback
objects (this is what you are accessing when you write todo.all
):
module.exports = {
all: function(req, res){
res.send('All todos')
},
viewOne: function(req, res){
console.log('Viewing ' + req.params.id);
},
create: function(req, res){
console.log('Todo created')
},
destroy: function(req, res){
console.log('Todo deleted')
},
edit: function(req, res){
console.log('Todo ' + req.params.id + ' updated')
}
};
这篇关于Route.get()需要回调函数,但会收到"object undefined"(对象未定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!