使用 knex 查询 return 为未定义

Query return as undefined using knex

我需要注册一个新用户,在收到参数时使用城市名称进行查询以获取州和城市 ID(均为外键)。我实现了一个函数来查找 id。在使用 data.id 的函数内部,id 被正确地 returned。但是在插入数据库的时候正在插入"undefined".

显然,保存操作在 findCity 和 findState 函数 return 值之前执行。

execution flow

cidade = 城市,estado = 城市

module.exports = app => {
    const obterHash = (senha, callback) => {
        bcrypt.genSalt(10, (err, salt) => {
            bcrypt.hash(senha, salt, null, (err, hash) => callback(hash))
        })
    }
    var idCidade;
    var idEstado
    function findCidade(cidade, ) {
        app.db('cidades')
        .where({ nome: cidade })
        .first()
        .then(data => {
            idCidade = data.id
            console.log('inside findCity. data.id: '+data.id)
        }).catch((err) => console.log("erro cidade", err));
    return
    }

    function findEstado(uf) {
        app.db('estados')
        .where({ uf: uf })
        .first()
        .then(data => {
            idEstado = data.id
            console.log('inside findState. data.id: '+data.id)
        }).catch((err) => console.log("erro estado", err));
    }

    const save = (req, res) => {
        console.log("\n")
        findCidade(req.body.cidade)
        findEstado(req.body.uf)

        obterHash(req.body.senha, hash => {
            const senha = hash
            console.log("Will be inserted. idCity: "+idCidade+" idState: "+idEstado)
            app.db('salao')
                .insert({ idcidade: idCidade,
                          idestado: idEstado,
                          senha})
                .then(_ => res.status(200).send())
                .catch(err =>{res.status(400).json(err)})

        })
    }
    return { save }
}

我来自巴西,我正在使用翻译器,对于拼写错误,我们深表歉意。

欢迎您来到异步世界!

一般说明:您将在它发生之前使用数据库查询的结果。您的程序必须等待结果 (idCidade, idEstado) 才能使用它。因此,您可以在日志中首先找到记录 Will be inserted...

为了解释,我将使用 Minimal Reproducible Example

function findCidade(cidade) {
  return Promise.resolve(1);
}

function findEstado(uf) {
  return Promise.resolve(1);
}

Promise.all([findCidade(), findEstado()])
  .then((data) => console.log(data));

输出为:

[ 1, 1 ]

要解决此问题,您必须:

  1. Return 使用 return 声明明确承诺。
  2. Awaitasync/awaitPromise 接口方法得到的结果。或者,如果更适合您,请使用 callbacks
module.exports = app => {
  const obterHash = (senha, callback) => {
    bcrypt.genSalt(10, (err, salt) => {
      bcrypt.hash(senha, salt, null, (err, hash) => callback(hash))
    })
  };

  function findCidade(cidade, ) {
    return app.db('cidades')
      .where({ nome: cidade })
      .first()
      .then(data => {
        idCidade = data.id
        console.log('inside findCity. data.id: '+data.id)
      }).catch((err) => console.log("erro cidade", err));
  }

  function findEstado(uf) {
    return app.db('estados')
      .where({ uf: uf })
      .first()
      .then(data => {
        idEstado = data.id
        console.log('inside findState. data.id: '+data.id)
      }).catch((err) => console.log("erro estado", err));
  }

  const save = (req, res) => {
    console.log("\n");

    Promise.all([findCidade(req.body.cidade), findEstado(req.body.uf)])
      .then((data) => {
        const [idCidade, idEstado] = data;
        obterHash(req.body.senha, hash => {
          const senha = hash;

          console.log("Will be inserted. idCity: "+idCidade+" idState: "+idEstado);
          app.db('salao')
            .insert({ idcidade: idCidade,
              idestado: idEstado,
              senha})
            .then(_ => res.status(200).send())
            .catch(err =>{res.status(400).json(err)})

        })
      })
      .catch((err) => console.log("general error", err));
  };
  return { save }
}