postman 关于KNEX

jbose2ul  于 2023-11-18  发布在  Postman
关注(0)|答案(1)|浏览(179)

我是Knex的新手,我尝试了一些交易。当我发送电子邮件地址到用户表时,它发送的像{"email":" [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) "},我迷路了,有人能帮助我吗?

  1. app.post('/register', (req, res) =>{
  2. const {email, name, password } = req.body;
  3. const hash = bcrypt.hashSync(password);
  4. db.transaction(trx =>{
  5. trx.insert ({
  6. has: hash,
  7. email: email
  8. })
  9. .into('login')
  10. .returning('email')
  11. .then (loginEmail => {
  12. return trx('users')
  13. .returning('*')
  14. .insert({
  15. email:loginEmail[0],
  16. name: name,
  17. joined: new Date()
  18. }).then(user =>{
  19. res.json(user[0]);
  20. })
  21. })
  22. .then(trx.commit)
  23. .catch(trx.rollback)`
  24. })
  25. })

字符串

hmtdttj4

hmtdttj41#

在第二个插入中插入了loginEmail[0]
你应该插入的是loginEmail[0].email,如果不是,你在技术上插入的对象看起来像{ email: ' [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) ' }

  1. app.post('/register', (req, res) => {
  2. const { email, name, password } = req.body;
  3. const hash = bcrypt.hashSync(password);
  4. db.transaction(trx => {
  5. trx.insert ({
  6. hash, // make sure you fix this spelling. also, in modern JS it's enough writing like this (no need for email: email)
  7. email
  8. })
  9. .into('login')
  10. .returning('email')
  11. .then (dbResponse => {
  12. return trx('users')
  13. .returning('*')
  14. .insert({
  15. email: dbResponse[0].email,
  16. name,
  17. joined: new Date(),
  18. }).then(user => {
  19. res.json(user[0]);
  20. })
  21. })
  22. .then(trx.commit)
  23. .catch(trx.rollback)`
  24. })
  25. })

字符串
我冒昧地添加了一些格式,并替换了dbResponse的变量,使其稍微清晰一些。

展开查看全部

相关问题