无法从函数返回对象Node.js

时间:2020-07-15 03:37:09

标签: javascript node.js json function object

我使用Node.js 创建一个简单的函数之后 我很惊讶此功能的结果是 undefined 但是在函数中,我尝试了console.log(my_value);并且输出是正确的值,没有任何问题 所以。我怎么了承诺问题或什么:/ 哦,我忘了..这是我在 Stackoverflow.com 上的第一篇文章:)

功能:

     client.get = async order => {
    if (!order) return console.error("ERROR: There is no entry order.");
    console.log("Get order!!!");
    try {
      await client.sql("CREATE TABLE IF NOT EXISTS Json (data JSON)").then(() => {
        client.sql("SELECT * FROM Json LIMIT 1").then(async row => {
          if (!row.length) {
            return false;
          } else {
            console.log("Inside function: " + getValue(JSON.parse(row[0].data), order));
            return getValue(JSON.parse(row[0].data), order); // return value
          }
        });
      });
    } catch (error) {
      console.error(error);
      return false;
    }
  };
};

控制台日志:

    Get command
    
    Get order!!!
    
    Inside function: Test123

    From command | await / async : undefined

    Get order!!!
    
    Inside function: Test123

    From command | then : undefined

命令:

    if (message.content == ".run") {
        console.log("Get command");
        await client.get("messages").then(d => { // test wtih then
          console.log(`From command | then : ${d}`); // test from command :/
        });
    
        let mydata = await client.get("messages"); // test with Async / Await
        console.log(`From command | await : ${mydata}`); // test from command :/
        return;
      }

使用的包裹:

  1. mysql |版本:v2.18.1 |使用Promise功能

  2. 获得价值|版本:v3.0.1

  3. discord.js |版本:v12.2.0

1 个答案:

答案 0 :(得分:0)

我认为您的代码非常复杂。

首先:然后不要使用async / await。

第二:返回错误

  return getValue(JSON.parse(row[0].data), order);

是查询client.sql("SELECT * FROM Json LIMIT 1")的返回值。函数client.get = async order没有返回值

可以尝试。
解决方案1:return new Promise

client.get = order => {
  return new Promise((resolve, reject) => {
    if (!order) reject(Error('ERROR: There is no entry order.'));
    console.log("Get order!!!");
    try {
      client.sql("CREATE TABLE IF NOT EXISTS Json (data JSON)").then(() => {
        return client.sql("SELECT * FROM Json LIMIT 1").then(row => {
          if (!row.length) {
            return false;
          } else {
            console.log("Inside function: " + getValue(JSON.parse(row[0].data), order));
            return resolve(JSON.parse(row[0].data), order); // return value
          }
        });
      })
    } catch (error) {
      console.error(error);
      return false;
    }
  })
};

解决方案2:异步/等待


client.get = async order => {
  if (!order) return "ERROR: There is no entry order.";
  console.log("Get order!!!");
  try {
    await client.sql("CREATE TABLE IF NOT EXISTS Json (data JSON)");
    const row = await client.sql("SELECT * FROM Json LIMIT 1")
    if (!row.length) return false;
    console.log("Inside function: " + getValue(JSON.parse(row[0].data), order));
    return getValue(JSON.parse(row[0].data), order); // return value
  } catch (error) {
    console.error(error);
    return false;
  }
};