我的支票状态和余额一直返回未定义,即使帐户处于活动状态且余额为 0。有什么办法可以纠正这个问题吗?

My check status and balance keeps returning undefined even though the account is active and balance is 0. Is there anyway to correct this?

提问人:Hephzy 提问时间:11/11/2023 最后编辑:ShadowHephzy 更新时间:11/11/2023 访问量:25

问:

我目前正在尝试构建一个 API,但我在检查帐户余额和状态方面遇到了问题。即使在我的数据库中,帐户已打开,余额为零,它们仍返回 undefined。

这是代码。

// CHECK IF ACCOUNT EXISTS AND IS ACTIVE
async function checkStatus(accountNumber) {
  const query = `
    SELECT Status
    FROM Accounts
    WHERE Account_No = ? AND Status = 1
  `;
  const values = [accountNumber];
  const result = (await dB).query(query, values);
  return result[0];
}

// CHECK IF ACCOUNT HAS ENOUGH MONEY TO TRANSACT
async function checkBalance(accountNumber) {
  const query = `
    SELECT Balance
    FROM Accounts
    WHERE Account_No = ?
  `;
  const values = [accountNumber];
  const result = (await dB).query(query, values);
  console.log(result);
  return result[0]; // Extract the balance from the result
}

// WITHDRAWAL FROM AN ACCOUNT
async function withdrawal(payload, id) {

  const { error, value } = schema.withdrawalSchema.validate(payload);

  if (error) {
    console.log(error.details, error.message)
    throw Error (error)
  }

  const { Amount, Source_account } = value;

  try {
    const accActive = await checkStatus(Source_account)
    console.log(accActive);

    if (!accActive) {
      logger.error(`This account cannot transact`)
      throw Error(`This account cannot transact`)
    } 

    if (id == Source_account) {

      const hasAmount = await checkBalance(Source_account)
      console.log(hasAmount);

      if (hasAmount >= Amount) {
        logger.error(`Not Enough Balance`)
        throw new InsufficientError(message);
      }

      const query = `
          INSERT INTO withdrawals (Transaction_id, Amount, Source_account)
          VALUES (?, ?, ?) 
        `;

      // Generate a UUID (version 4)
      const generatedUUID = uuid.v4();

      // Remove hyphens and take the first 16 digits
      const transaction_id = generatedUUID.replace(/-/, '').slice(0, 16);

      console.log(transaction_id)

      // Insert the transaction id into the database
      const value = [transaction_id, Amount, Source_account]
      const answer = (await dB).query(query, value);

      if (answer) {

        const amountQuery = `
          UPDATE Accounts
          SET Balance = Balance - ? , Last_Updated = CURRENT_TIMESTAMP
          WHERE Account_No = ?
        `;
        const values = [Amount, Source_account]
        const result = (await dB).query(amountQuery, values)

        return result;
      } else {
        const withdrawQuery = `
          UPDATE withdraws
          SET Status = Failed
          WHERE Transaction_id = ? 
        `;

        const value = [transaction_id]
        const output = (await dB).query(withdrawQuery, value)
        
        logger.warn(`Withdraw failed`)
        return false;
      }

    } else {
      throw new AccNotMatchError(message)
    }
  } catch (error) {
    throw Error(error.message)
  }
}

如果应该运行,因为该帐户处于活动状态,但它返回错误帐户无法交易并且不应该运行,因为帐户余额为 0,但它仍然运行并给我一个负余额accActivehasAmount >= Amount

MySQL 节点.js Express

评论

0赞 Shadow 11/11/2023
请将问题中的代码缩小到最低限度,并准确标记您遇到错误的位置。
0赞 VLAZ 11/12/2023
不应是 (if is not a promise) 或 (if is a promise.虽然如果不是更好)?(await dB).query(query, values);await dB.query(query, values);dBawait (await dB).query(query, values);dB
0赞 Hephzy 11/13/2023
await db.query(query, values);抛出错误。我将尝试您提到的另一种方式,但它的工作方式与我实现它的方式相同。@VLAZdB isn't a promise

答: 暂无答案