Solution to the problem of loss of precision due to too long id obtained

Solution to the problem of loss of precision due to too long id obtained

When writing an interface in node.js, I encountered an interesting problem.

Get the interface for all dishes

// 查询所有菜品
exports.getAllDish = (req,res)=>{
    
    
  const sql = `select * from dish`
  db.query(sql,(err,results)=>{
    
    
    if(err) return res.cc(err)
    return res.send({
    
    
      status:0,
      message:'获取菜品成功!',
      data:results
    })
  })
}

operation result:
operation result

Get query dishes based on id

// 根据id查询菜品
exports.getDishById = (req,res)=>{
    
    
  const sql = `select * from dish where id=?`
  console.log(req.params.id)
  db.query(sql,req.params.id,(err,results)=>{
    
    
    console.log('results',results)
    if(err) return res.cc(err)
    if(results.length !== 1) return res.cc('获取菜品失败了噢!')
    return res.send({
    
    
      status:0,
      message:'获取菜品成功!',
      data:results[0]
    })
  })

Running results:
operation result
The result is empty
It's amazing. Why is it that the data was obtained just now, but this time the result based on the copied id is empty?
I went to check the tables in the database and
database
was shocked to see that the information in the database was different from the traversal results! ! !
It turns out that the number type ID is too long. The browser resolves the data and omits all subsequent content for data exceeding 2 to the 53rd power, resulting in inconsistent IDs. The solution:

  1. When configuring the field type in the background interface, change the field type of id to string type.
  2. Change the length of id to shorter length.
    Solve the problem:
    operation result

Guess you like

Origin blog.csdn.net/weixin_51938823/article/details/132080034