简体   繁体   English

无法读取 Express js 中未定义的属性“状态”

[英]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?为什么不尝试将reqres作为输入参数传递给findAllUsers方法?

router.get('/users', (req, res) => {
    controller.findAllUsers(req, res);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js、express.js 和 mysql - 无法读取未定义的属性“状态” - node.js, express.js, and mysql - Cannot read property 'status' of undefined 无法读取未定义的 sequelize express JS 的属性“findAll” - Cannot read property 'findAll' of undefined sequelize express JS Express.js 无法读取未定义的属性“req” - Express.js Cannot read property 'req' of undefined 如何解决无法读取express js中未定义的属性“名称”? - how to solve Cannot read property 'name' of undefined in express js? 类型错误:无法读取未定义的属性“forEach”(节点 js、stripe、express) - TypeError: Cannot read property 'forEach' of undefined (node js, stripe, express) “类型错误:无法读取未定义的属性‘状态’”node.js - "TypeError: Cannot read property 'status' of undefined" node.js 无法读取未定义 Express API 的属性“计数” - Cannot read property 'count' of undefined Express API 错误无法读取未定义 Express 应用程序的属性 - Error cannot read property of undefined Express app React Express TypeError:无法读取未定义的属性“ then” - React Express TypeError: Cannot read property 'then' of undefined 无法读取未定义的属性-NodeJS,Express,Mongoose - Cannot read property of undefined - NodeJS, Express, Mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM