最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

出现此错误 CastError:路径“balance”处的值“NaN”(类型编号)转换为数字失败计算无法正常工作

网站源码admin41浏览0评论

出现此错误 CastError:路径“balance”处的值“NaN”(类型编号)转换为数字失败计算无法正常工作

出现此错误 CastError:路径“balance”处的值“NaN”(类型编号)转换为数字失败计算无法正常工作

我有这个问题摆在我面前。

当我这样运行时,它给了我一个积极的回应。没有问题。

router.post("/us-transfer", async (req, res, next) => {
  try {
    if (
      !req.headers.authorization ||
      !req.headers.authorization.startsWith("Bearer ") ||
      !req.headers.authorization.split(" ")[1]
    ) {
      return res.status(422).json({ message: "Please Provide Token!" });
    }

    var source_currency = 'NGN';
    var amount = req.body.amount;
    var narration = req.body.narration;
    var currency = req.body.currency;
    var beneficiary_name = req.body.beneficiary_name;
    var AccountNumber = req.body.AccountNumber;
    var RoutingNumber = req.body.RoutingNumber;
    var SwiftCode = req.body.SwiftCode;
    var BankName = req.body.BankName;
    var BeneficiaryAddress = req.body.BeneficiaryAddress;
    var BeneficiaryCountry = req.body.BeneficiaryCountry;

    url = "";
    await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json",
        Authorization:
          "Bearer FLWSECK_TEST-153740d351951b5f6d5ae8b903e0c467-X",
      },
      body: JSON.stringify({
        amount: amount,
        narration: narration,
        currency: currency,
        beneficiary_name: beneficiary_name,
        meta: [
          {
            AccountNumber: AccountNumber,
            RoutingNumber: RoutingNumber,
            SwiftCode: SwiftCode,
            BankName: BankName,
            BeneficiaryName: beneficiary_name,
            BeneficiaryAddress: BeneficiaryAddress,
            BeneficiaryCountry: BeneficiaryCountry,
          },
        ],
      }),
    })
      .then((response) => response.json())
      .then(async (json) => {
        const debit = await User.find({ email: req.body.email });
        const debit_balance = debit[0].balance;
        const debit_amt = debit_balance - amount;
        await User.findOneAndUpdate(
          { email: req.body.email },
          { $set: { balance: debit_amt } }
        );

        const transactions = new Trans({
          email: req.body.email,
          narration: narration,
          credit: 0.0,
          debit: amount,
          amount: amount,
        });
        try {
          SendFXTransferNotification(req.body.email, beneficiary_name, amount);
          transactions.save();
          //res.send(savedUser);
          console.log("transaction saved");
        } catch (err) {
          //res.status(400).send(err);
          console.log(err);
        }

        //sendTransferConfirmation(email);
        return res.send({ error: false, data: json, message: "OK" });
      })
      .catch((err) => {
        throw err;
      });
  } catch (err) {
    next(err);
  }
});

问题来了。我写这个函数来执行货币兑换

const GetEqCurrency = async (source_currency, destination_currency, amount) => {
  var header = {
    Accept: "application/json",
    "Content-type": "application/json",
    "Authorization":
      "Bearer FLWSECK_TEST-153740d351951b5f6d5ae8b903e0c467-X",
  };

  var url = `=${amount}&destination_currency=${destination_currency}&source_currency=${source_currency}`;

  try{
    const response = await fetch(url, {method: 'GET', headers:header});
    const responseJson = await response.json();
    if(responseJson.message == "Transfer amount fetched"){
      return responseJson.data.source.amount;
    }
  }catch(error)
  {
    console.log(error)
  }
};

它会返回一个错误。

如果我的代码看起来像这样

router.post("/us-transfer", async (req, res, next) => {
  try {
    if (
      !req.headers.authorization ||
      !req.headers.authorization.startsWith("Bearer ") ||
      !req.headers.authorization.split(" ")[1]
    ) {
      return res.status(422).json({ message: "Please Provide Token!" });
    }

    var source_currency = 'NGN';
    var amount = req.body.amount;
    var narration = req.body.narration;
    var currency = req.body.currency;
    var outAmt = GetEqCurrency(source_currency, req.body.currency, amount); //Added this
    var beneficiary_name = req.body.beneficiary_name;
    var AccountNumber = req.body.AccountNumber;
    var RoutingNumber = req.body.RoutingNumber;
    var SwiftCode = req.body.SwiftCode;
    var BankName = req.body.BankName;
    var BeneficiaryAddress = req.body.BeneficiaryAddress;
    var BeneficiaryCountry = req.body.BeneficiaryCountry;

    url = "";
    await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json",
        Authorization:
          "Bearer FLWSECK_TEST-153740d351951b5f6d5ae8b903e0c467-X",
      },
      body: JSON.stringify({
        amount: outAmt, // used outAmt variable here 
        narration: narration,
        currency: currency,
        beneficiary_name: beneficiary_name,
        meta: [
          {
            AccountNumber: AccountNumber,
            RoutingNumber: RoutingNumber,
            SwiftCode: SwiftCode,
            BankName: BankName,
            BeneficiaryName: beneficiary_name,
            BeneficiaryAddress: BeneficiaryAddress,
            BeneficiaryCountry: BeneficiaryCountry,
          },
        ],
      }),
    })
      .then((response) => response.json())
      .then(async (json) => {
        const debit = await User.find({ email: req.body.email });
        const debit_balance = debit[0].balance;
        const debit_amt = debit_balance - outAmt; // used outAmt variable here
        await User.findOneAndUpdate(
          { email: req.body.email },
          { $set: { balance: debit_amt } }
        );

        const transactions = new Trans({
          email: req.body.email,
          narration: narration,
          credit: 0.0,
          debit: amount,
          amount: amount,
        });
        try {
          SendFXTransferNotification(req.body.email, beneficiary_name, amount);
          transactions.save();
          //res.send(savedUser);
          console.log("transaction saved");
        } catch (err) {
          //res.status(400).send(err);
          console.log(err);
        }

        //sendTransferConfirmation(email);
        return res.send({ error: false, data: json, message: "OK" });
      })
      .catch((err) => {
        throw err;
      });
  } catch (err) {
    next(err);
  }
});

错误看起来是这样

Apr 24 12:00:38 PM  POST /api/user-serv/us-transfer 500 833.426 ms - 148
Apr 24 12:00:38 PM  CastError: Cast to Number failed for value "NaN" (type number) at path "balance"
Apr 24 12:00:38 PM      at model.Query.exec (/opt/render/project/src/node_modules/mongoose/lib/query.js:4933:21)
Apr 24 12:00:38 PM      at model.Query.Query.then (/opt/render/project/src/node_modules/mongoose/lib/query.js:5032:15)
Apr 24 12:00:38 PM      at processTicksAndRejections (internal/process/task_queues.js:95:5)

我做错了什么?我没有看到任何看起来更像是错误的东西,但与此同时,它仍然无法正常工作。在互联网上搜索了所有内容,但仍然没有意义。

回答如下:
发布评论

评论列表(0)

  1. 暂无评论