3

我是异步/等待的新手。

我正在尝试使用异步和等待,但查询没有等待,它最后发生并且页面在查询之前呈现,所以我无法在呈现的页面上得到正确的答案。

这是我使用异步等待之前的代码

orderMiddleware.newOrder = function (req, res) {
    var total = 0
    var curr_total = 0
    // get items from cart
    c.query('select * from cart where user_id=:userId',
        { userId: req.user.ID }, function (err, cart) {
            if (err) {
                console.log(err)
            } else {
                cart.forEach(function (item) {
                    // Find item from DB and check their price
                    c.query('select * from products where id=:id',
                        { id: item.item_id },
                        function (err, foundItem) {
                            if (err) {
                                console.log(err)
                            } else {
                                curr_total = foundItem[0].price * item.quantity
                                console.log("currenttotal" + curr_total)
                                total += curr_total
                                console.log(total)
                            }
                        })
                })
                console.log(total)
                console.log(curr_total)
                // Calculate total price
                // Multiply all items with their quantity
                res.render('orders/new', { cart: cart, total: total })
            }
        })
}

但是,这不能正常工作。console.log(total) 发生在查询之前,因此结果为零,并且在呈现的页面中呈现为零。如果我使用异步,也会发生同样的事情。我用错了吗?

使用异步等待后-

orderMiddleware.newOrder = async (req, res) => {
    var total = 0
    var curr_total = 0
    // get items from cart
   var A=  c.query('select * from cart where user_id=:userId',
        { userId: req.user.ID }, async (err, cart) => {
            if (err) {
                console.log(err)
            } else {
                 cart.forEach(async (item) => {
                    // Find item from DB and check their price
                    await c.query('select * from products where id=:id',
                        { id: item.item_id },
                        async (err, foundItem) =>{
                            if (err) {
                                console.log(err)
                            } else {
                                curr_total = foundItem[0].price * item.quantity
                                console.log("currenttotal" + curr_total)
                                total += curr_total
                                console.log(total)
                            }
                        })
                })
                await console.log(total)
                // await console.log(curr_total)
                // Calculate total price
                // Multiply all items with their quantity
                await res.render('orders/new', { cart: cart, total: total })
            }
        })
}

我尝试不使用回调,例如:

var A=  c.query('select * from cart where user_id=:userId',
        { userId: req.user.ID })

但是我怎样才能得到查询的输出呢?console.log(A) 显示不同的结果。

4

3 回答 3

4

你不能因为函数不返回承诺。您可以使用包含 30 部分的库(例如es6-promisify )来承诺这些功能,或者您可以自己包装这些功能。

一旦一个函数返回一个 Promise,你就可以等待它。

例如,对于上述情况,解决方案可能如下:

const execQuery = (sql, params) => new Promise((resolve, reject) => {
  query(sql, params, (error, data) => {
    if (error) {
      reject(error);
    } else {
      resolve(data);
    }
  });
});

const logCartItem = async (userId) => {
  try {
    const items = await execQuery('select * from cart where user_id=:userId', { userId });
    items.forEach(console.log);
  } catch (error) {
    console.error(error);
  }
};
于 2017-07-23T10:56:55.227 回答
1

假设您正在使用该node-mariasql软件包。简短的回答是你不能使用async/await,因为包不支持 Promises

于 2017-07-22T21:36:44.377 回答
0

使用 node-mariasql 很容易使用 promisify

const util = require('util')

const asyncQuery = util.promisify(c.query);

const rows = await asyncQuery.call(c, 'SELECT product FROM products WHERE id = :id', { id }, { useArray: false, metaData: false })
于 2019-01-06T21:53:43.983 回答