postman 关于KNEX

jbose2ul  于 7个月前  发布在  Postman
关注(0)|答案(1)|浏览(53)

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

app.post('/register', (req, res) =>{
     const {email, name, password } = req.body;
     const hash = bcrypt.hashSync(password);
       db.transaction(trx =>{
          trx.insert ({
              has: hash,
              email: email
          })
          .into('login')
          .returning('email')
          .then (loginEmail => {
             return trx('users')
              .returning('*')
              .insert({
                email:loginEmail[0],
                  name: name,
                  joined: new Date()
            }).then(user =>{
                res.json(user[0]);
                        })
        })
        .then(trx.commit)
        .catch(trx.rollback)`
    })
})

字符串

hmtdttj4

hmtdttj41#

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

app.post('/register', (req, res) => {
     const { email, name, password } = req.body;
     const hash = bcrypt.hashSync(password);
       db.transaction(trx => {
          trx.insert ({
              hash, // make sure you fix this spelling. also, in modern JS it's enough writing like this (no need for email: email)
              email
          })
          .into('login')
          .returning('email')
          .then (dbResponse => {
             return trx('users')
              .returning('*')
              .insert({
                email: dbResponse[0].email,
                  name,
                  joined: new Date(),
            }).then(user => {
                res.json(user[0]);
                        })
        })
        .then(trx.commit)
        .catch(trx.rollback)`
    })
})

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

相关问题