简体   繁体   English

创建新用户时出现数据类型错误 Knex Postgresql Node.js

[英]Data Type Error when create a new user Knex Postgresql Node.js

I have this createUser method:我有这个 createUser 方法:

const createUser = (request, response) => {
   const { username, password, score } = request.body
   
   database("user")
   .select()
   .where({ username })
   .first()
   .then(user => {
       if (!user) {
           return bcrypt.hash(password, 12)
               .then(hashedPassword => {
                   return database("user").insert({
                       username, password_digest: hashedPassword, score
                   }).returning("*")
               })
               .then(users => {
                   const secret = "HERESYOURTOKEN"
                   jwt.sign(users[0], secret, (error, token) => {
                   console.log(users[0])
                   response.json({ token, user: users[0] })
                   })
               })
       }
           response.send("Please choose another username")
   }).catch(error => {
       response.status(401).json({
           error: error.message
       })
   })
   
}

And this Authentication method:而这个身份验证方法:

function authenticate(request, response, next){
    const token  = request.headers.authorization.split(" ")[1]
    const secret = "HERESYOURTOKEN";
    if (!token) {
        response.sendStatus(401)
    }
    let id = null
    try {
        id = jwt.verify(token, secret)
    } catch(error){
        response.sendStatus(403)
    }
    const user = database("user")
        .select()
        .where("id", id)
        .first()

    request.user = user;
    next();
}

And when I use the Postman to create a new user it gives me 401 Unauthorized error with message: enter image description here当我使用 Postman 创建新用户时,它给了我 401 Unauthorized 错误消息:在此处输入图像描述

The score data type is integer:分数数据类型为 integer:

exports.up = function(knex) {
  return knex.schema.createTable("user", table => {
      table.increments()
      table.string("username")
      table.string("password_digest")
      table.integer("score")
  })
};

exports.down = function(knex) {
  return knex.schema.dropTableIfExists("user")
};

Probably someone can help me with it.可能有人可以帮助我。

The error should be from jwt.sign() where your secret is not in a right format.错误应该来自jwt.sign()您的秘密格式不正确。

Can you try a simple const secret = "ABC" to see if it works?你能尝试一个简单的const secret = "ABC"看看它是否有效吗?

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

相关问题 Node.js - PostgreSQL - 无法确定参数 $1 的数据类型错误 - Node.js - PostgreSQL - could not determine data type of parameter $1 error 当我尝试使用knex(Node.js)将数据插入数据库时​​发布错误 - Post error when i try to insert data into Database with knex (Node.js) 按下新用户时,node.js 出现错误 - node.js getting error when pressing new user PUT 请求不运行 Knex 更新语法并且不引发任何错误,继续加载 POSTMAN。 (Knex.js、PostgreSQL、Node.js) - PUT Request Does Not Run Knex Update Syntax And Does Not Throw Any Error, Keeps Loading In POSTMAN. (Knex.js , PostgreSQL, Node.js) 带有knex的node.js-提交完成时(mysql db) - node.js with knex - when commit is done (mysql db) Node.js + LDAP-从Android创建新用户 - Node.js + LDAP - Create new user from Android 检查用户是否在 node.js 中的 postgresql (异步) - Check if user is in postgresql in node.js (async) 在node.js中创建插入函数(postgresql) - Create insert function (postgresql) in node.js 如何让用户在Node.js上生成的数据中输入数据 - How to let user type in data into a spawned process on Node.js 如何使用 Node.js 和 Knex 连接到 AWS EC2 实例内的 postgresql 数据库 - How to connect to postgresql database inside AWS EC2 instance using Node.js and Knex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM