[英]Cannot read property 'status' of undefined in Express js
Here is my code:这是我的代码:
const db = require('../models');
const findAllUsers = async (req, res) => {
try {
const users = await db.User.findAll();
return res.status(200).json({
users,
});
} catch (error) {
return res.status(500).json({error: error.message})
}
}
module.exports = {
findAllUsers,
}
While executing this am getting an error saying "TypeError: Cannot read property 'status' of undefined"执行此操作时收到错误消息“TypeError:无法读取未定义的属性‘状态’”
Am using, express js and sequelize to perform the action !正在使用,表达 js 和 sequelize 来执行操作!
this is my route这是我的路线
router.get('/users', (req, res) => {
//const a = controller.allUsers;
res.send(controller.findAllUsers());
});
TypeError: Cannot read property 'status' of undefined类型错误:无法读取未定义的属性“状态”
What are you accessing status
on which might be undefined?你在访问status
上可能是不确定的?
res.status
So what is res
and why might be undefined?那么什么是res
以及为什么可能未定义?
const findAllUsers = async (req, res) => {
res
is the second argument you pass to findAllUsers
res
是您传递给findAllUsers
的第二个参数
controller.findAllUsers()
… and you aren't passing any arguments to findAllUsers
so they will both be undefined. ...你不传递任何参数来findAllUsers
所以他们都将是不确定的。
You need to pass values to the arguments you use!您需要将值传递给您使用的参数!
Aside:在旁边:
res.send(controller.findAllUsers());
You're taking the return value of findAllUsers
and sending it in the response … but you're already trying to send a response inside that function.您正在获取findAllUsers
的返回值并将其发送到响应中……但您已经尝试在该函数内发送响应。 You can't respond multiple times to the same request.您不能多次响应同一个请求。
Why don't you try to pass req
and res
as input parameter to the findAllUsers
method?为什么不尝试将req
和res
作为输入参数传递给findAllUsers
方法?
router.get('/users', (req, res) => {
controller.findAllUsers(req, res);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.