Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Sequelize`findOne`工作不符合预期_Node.js_Typescript_Postgresql_Sequelize.js_Next.js - Fatal编程技术网

Node.js Sequelize`findOne`工作不符合预期

Node.js Sequelize`findOne`工作不符合预期,node.js,typescript,postgresql,sequelize.js,next.js,Node.js,Typescript,Postgresql,Sequelize.js,Next.js,我遇到了一个一周都没能解决的问题 当我将用户的电子邮件传递给数据库的查询时,sequelize似乎做得很好并执行此查询,它从表中返回数据并返回找到的结果,但是执行findOne函数的结果不是一个模型,为什么会发生这种情况,我如何修复它 const-email=”email@example.com' const user=await user.findOne({where:{email}}) console.log(user.password)//未定义 下面是模型、服务和数据库连接 型号 从“

我遇到了一个一周都没能解决的问题

当我将用户的电子邮件传递给数据库的查询时,sequelize似乎做得很好并执行此查询,它从表中返回数据并返回找到的结果,但是执行
findOne
函数的结果不是一个模型,为什么会发生这种情况,我如何修复它

const-email=”email@example.com'
const user=await user.findOne({where:{email}})
console.log(user.password)//未定义
下面是模型、服务和数据库连接

型号

从“sequelize”导入{DataTypes,Model}
从“/database”导入{connection}
类用户扩展模型{
只读id:number
只读电子邮件:字符串
密码:string
用户名:string
只读createdAt:日期
只读更新日期:日期
}
User.init(
{
身份证:{
类型:DataTypes.INTEGER,
自动递增:真,
primaryKey:没错,
},
电邮:{
类型:DataTypes.STRING,
独一无二:没错,
},
密码:{
类型:DataTypes.STRING,
},
用户名:{
类型:DataTypes.STRING,
},
},
{
续集:连接,
型号名称:“用户”,
createdAt:“已创建”,
updatedAt:“已更新”,
}
)
导出默认用户
服务

导出异步函数登录用户(电子邮件:字符串,密码:字符串):承诺{
试一试{
const user=await user.findOne({where:{email}})
如果(!用户){
抛出错误(“未找到用户”)
}
如果(!bcrypt.compareSync(密码,用户密码)){
抛出错误(“未找到用户或密码不匹配”)
}
const{password:,…payload}=user.toJSON()
const-token=jwt.sign(有效负载,environment.jwt)
返回{success:true,content:token}
}捕获(错误){
返回{success:false,error:error.message}
}
}
数据库连接

从“Sequelize”导入{Sequelize}
从“./environment”导入{environment}
export const connection=new Sequelize(environment.database{
方言:“博士后”,
日志记录:console.log,
选择:{
ssl:{
拒绝:错误,
},
},
})
这是日志

Executing (default): SELECT "id", "email", "password", "username", "created_at", "updated_at" FROM "users" AS "users" WHERE "users"."email" = 'email@example.com';
users {
  dataValues: {
    id: 9,
    email: 'email@example.com',
    password: '$2b$10$s0HFL9eBbcp3GcGkB9cDZuSiCjOEgfQB5lZLxhxXraRfvRF6voDfW',
    username: null,
    created_at: 2020-11-16T13:09:30.631Z,
    updated_at: 2020-11-16T13:09:30.631Z
  },
  _previousDataValues: {
    id: 9,
    email: 'email@example.com',
    password: '$2b$10$s0HFL9eBbcp3GcGkB9cDZuSiCjOEgfQB5lZLxhxXraRfvRF6voDfW',
    username: null,
    created_at: 2020-11-16T13:09:30.631Z,
    updated_at: 2020-11-16T13:09:30.631Z
  },
  _changed: Set(0) {},
  _options: {
    isNewRecord: false,
    _schema: null,
    _schemaDelimiter: '',
    raw: true,
    attributes: [
      'id',
      'email',
      'password',
      'username',
      'created_at',
      'updated_at'
    ]
  },
  isNewRecord: false,
  id: undefined,
  email: undefined,
  password: undefined,
  username: undefined,
  createdAt: undefined,
  updatedAt: undefined
}
使用堆栈
NextJS、Sequelize、PostgreSQL


我做错了什么?

您是否检查了您的用户对象?它是否也未定义

尝试执行
user.dataValues.password
以获取密码。 或者您可以像这样添加
raw:true

const user=wait user.findOne({raw:true,其中:{email}})
。Sequelize将只返回数据,而不返回模型实例。因此,您可以使用
user.password

模型实例使用
dataValues
属性的概念进行操作,该属性存储实例表示的实际值。 正如您在
dataValues
下的打印输出中所看到的,您拥有您的模型:

Executing (default): SELECT "id", "email", "password", "username", "created_at", "updated_at" FROM "users" AS "users" WHERE "users"."email" = 'email@example.com';
users {
  dataValues: {
    id: 9,
    email: 'email@example.com',
    password: '$2b$10$s0HFL9eBbcp3GcGkB9cDZuSiCjOEgfQB5lZLxhxXraRfvRF6voDfW',
    username: null,
    created_at: 2020-11-16T13:09:30.631Z,
    updated_at: 2020-11-16T13:09:30.631Z
  },
  _previousDataValues: {
      (...)
要从该
dataValues
字段获取数据,可以使用
get
方法。 同时
findOne
返回承诺

综上所述,在你的情况下,我认为这应该是可行的

const email = 'email@example.com'
User.findOne({ where: { email }}).then(data => {
    console.log(data.get('password')
});

查看谢谢,答案是
Model.get({plain:true})
,然后再处理接收到的数据