async/await仍在执行前返回数据

pod7payv  于 2021-06-10  发布在  Cassandra
关注(0)|答案(2)|浏览(386)

我正在写一个聊天应用程序使用 Cassandra 以及 Node.Js . 我用过 async/awaitNode.Js . 但它不起作用。我的 app.js 代码段:

const cassandra = require('./routes/cassandra');
....
//I have used socket based on my use-case.
socket.on('room-available', async (roomName) => {
    let result = await cassandra.isRoomAvailable(roomName);
    console.log(`result: ${result}`);
    await socket.emit('room-available', result);
});

我的 cassandra.js 代码段:

var isRoomAvailable = async (roomName) => {
    var query = 'select room_id from chat_rooms where room_name = ? ALLOW FILTERING';
    //execute is cassandra's function.
    return await client.execute(query,[roomName],(error, result) => {
        if(error){
            console.log(`error in inserting: ${error}`);
            genericResponse['status'] = "error";
            genericResponse['message'] = error;
            genericResponse['responseObject'] = {};
            console.log(`generic response error: ${JSON.stringify(genericResponse)}`);
            return genericResponse;
        }
        else {
            console.log(`roomID: ${JSON.stringify(result.rows)}`);
            if(result.rows != null){
                genericResponse['status'] = "sucess";
                genericResponse['message'] = "True";
                console.log("GenericResponse - available: " , genericResponse);
                return genericResponse;
            }else{
                genericResponse['status'] = "sucess";
                genericResponse['message'] = "False";
                console.log("GenericResponse - not available: " , genericResponse);
                return genericResponse;
            }
        }
    }); 
}

问题:

result: undefined
roomID: [{"room_id":"0507a5e01097e04f117e8ac620957033b7d7f5ac"}, {"room_id":"0507a5e01097e04f117e8ac620957033b7d7f5ac"},{"room_id":"7eb482bc1cc8294399a90ae49ccf0f6d588a2aef"},{"room_id":"7eb482bc1cc8294399a90ae49ccf0f6d588a2aef"}]
GenericResponse - available:  { status: 'sucess', message: 'True', responseObject: '' }
``` `result` 先得到回报 `undefined` . 基本上, `result` 应该容纳所有的室友。请帮我解决这个问题。谢谢。
gg58donl

gg58donl1#

您使用的是基于回调的方法,它不返回承诺。

client.execute(query, [ 'someone' ], function(err, result) {});

尝试使用基于承诺的版本,例如:

const isRoomAvailable = async roomName => {
    const query = 'select room_id from chat_rooms where room_name = ? ALLOW FILTERING';
    try {
      const result = await client.execute(query, [ roomName ], { prepare: true });
    } catch (err) {
      // Do something here
    }
};

有关更多信息,请参阅文档:https://docs.datastax.com/en/developer/nodejs-driver/4.3/features/promise-callback/

31moq8wy

31moq8wy2#

如果您希望您的代码是异步的,请尝试使用以下格式

const isRoomAvailable = async roomName => {
  try {
    let query = "select room_id from chat_rooms where room_name = ? ALLOW FILTERING";
    let result = await client.execute(query, [roomName]).promise();
    console.log(`roomID: ${JSON.stringify(result.rows)}`);
    if (result.rows != null) {
      genericResponse["status"] = "sucess";
      genericResponse["message"] = "True";
      console.log("GenericResponse - available: ", genericResponse);
      return genericResponse;
    } else {
      genericResponse["status"] = "sucess";
      genericResponse["message"] = "False";
      console.log("GenericResponse - not available: ", genericResponse);
      return genericResponse;
    }
  } catch (e) {
    console.log(`error in inserting: ${error}`);
    genericResponse["status"] = "error";
    genericResponse["message"] = error;
    genericResponse["responseObject"] = {};
    console.log(`generic response error: ${JSON.stringify(genericResponse)}`);
    return genericResponse;
  }
};

相关问题