【问题标题】:how to console log the return value from promise Sequelize如何控制台记录 Promise Sequelize 的返回值
【发布时间】:2019-04-08 15:06:29
【问题描述】:

我正在使用 Sequelize,但无法记录承诺的价值。我正在做console.log('helooo', timekeep);,但它打印Promise { <pending> }。我不知道我做得对不对。

这是我的代码:

const create = async function(req, res) {
    res.setHeader('Content-Type', 'application/json');
    let err, orders, wallet;
    let orders_info = req.body;  
    console.log('order_info : <<<<<< ' + JSON.stringify(orders_info));
    var timekeep =  Orders.findAndCountAll(
    {
        where: {
          cid : orders_info.cid,
        },      
        order: [
          ['id', 'DESC']
        ],
        limit: 1,
      }
   )
   .then(result => {
       console.log('hello', result);
       console.log(result.count);
       console.log(result.rows);
   });
   console.log('helooo',  timekeep);
}

当我记录 var 时,我会变成这样。

helooo
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }

【问题讨论】:

    标签: mysql node.js sequelize.js


    【解决方案1】:

    timekeep 是您从then 子句返回的值,即Promise&lt;void&gt;,因为它没有返回任何值。

    您使用console.log('hello', result); 打印的result 具有您想要的Promise 值。

    【讨论】:

    • 是的,但我无法在函数外使用“结果”
    • 正确,因为它仅在 then 内部定义。如果您返回result 并在Orders.findAndCountAll 之前添加await,您将获得timekeep 的值
    【解决方案2】:

    我可以看到您的 create 函数已经是 async 函数。因此,不要使用 Promise 语法,而是使用 async/await 语法。现在您要做的是将查询表达式分配给变量timekeep。这是示例代码,

    const create = async function (req, res) {
        res.setHeader('Content-Type', 'application/json');
        let err, orders, wallet;
        let orders_info = req.body;
        console.log('order_info : <<<<<< ' + JSON.stringify(orders_info));
        // try resolving promise
        try {
            var timekeep = await Orders.findAndCountAll(
                {
                    where: {
                        cid: orders_info.cid,
                    },
                    order: [
                        ['id', 'DESC']
                    ],
                    limit: 1,
                }
            );
            console.log("RESULT => ", timekeep)
        } catch (err) {
            // catch error here
            console.log(err)
        }
    }
    

    【讨论】:

      【解决方案3】:

      返回数据和async await 概念存在一些小问题,请查看下面的代码并附上评论:

      var timekeep = await Orders.findAndCountAll({ // <----------- you need add await
          where: {
              cid: orders_info.cid,
          },
          order: [
              ['id', 'DESC']
          ],
          limit: 1,
      })
      .then(result => {
          console.log('hello', result);
          console.log(result.count);
          console.log(result.rows);
          return result; // <----------- also need to return the result from here
      });
      console.log('helooo', timekeep); // <----- Now check the console
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 1970-01-01
        • 2019-02-09
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 2023-01-08
        相关资源
        最近更新 更多