【问题标题】:Firebase Realtime Database Query is not working with larger numberFirebase 实时数据库查询不适用于更大的数字
【发布时间】:2021-05-29 15:10:29
【问题描述】:

我正在查询我的订单列表,这里是数据结构和我的数据库规则:

我使用这样的查询来确定最新价格是否达到了 tp 或 sl 价格,如下所示:

function GetBuyList(symbol, currentPrice) {
    //Less than or equal to, for buying search tp lesser than current price
    var buy_tp = db.ref(`orders/active_orders/${symbol}`).orderByChild("tp").startAt(`buy_tp_${0}`).endAt(`buy_tp_${currentPrice}`)

    //More than or equal to, for buying search sl higher than current price
    var buy_sl = db.ref(`orders/active_orders/${symbol}`).orderByChild("sl").startAt(`buy_sl_${currentPrice}`).endAt(`buy_sl_${100000000}`)

    buy_tp.once("value", function (snapshot) {
        // do some stuff once
        if (snapshot.val() !== null) {
            ProcessOrders(snapshot.val(), 'tpHit', currentPrice)
        }
    });

    buy_sl.once("value", function (snapshot) {
        // do some stuff once
        if (snapshot.val() !== null) {
            ProcessOrders(snapshot.val(), 'slHit', currentPrice)
        }
    });
}

对于像 1.211 这样的较低值的价格,它工作正常,但是当价格变大时,buy_sl 查询不起作用,但 buy_tp 查询仍然工作正常。例如,当我为下面的数据查询 34886 的价格时,buy_sl 不起作用:

编辑: 嗨弗兰克,这里导出的 json:

{
  "active_orders" : {
    "BTCUSD" : {
      "-Masii03kq9LvuLfWOyG" : {
        "close_type" : "None",
        "lot_size" : 1,
        "order_price" : 34888.17,
        "sl" : "buy_sl_34887",
        "symbol" : "BTCUSD",
        "tp" : "buy_tp_34889",
        "ts" : 1622301925456,
        "type" : "buy",
        "uid" : "6XaKYgXCsuMNg1d5bWYHg6ej5sd2"
      }
    },
    "EURUSD" : {
      "-MasVPCtD4sdPCcdF9S9" : {
        "close_type" : "None",
        "lot_size" : 1,
        "order_price" : 1.211,
        "sl" : "buy_sl_1.210",
        "symbol" : "EURUSD",
        "tp" : "buy_tp_1.23",
        "ts" : 1622298174339,
        "type" : "buy",
        "uid" : "6XaKYgXCsuMNg1d5bWYHg6ej5sd2"
      }
    },
    "USDJPY" : {
      "-MasWoRREHQhvOR6iQ8G" : {
        "close_type" : "None",
        "lot_size" : 1,
        "order_price" : 109.861,
        "sl" : "buy_sl_107.0",
        "symbol" : "USDJPY",
        "tp" : "buy_tp_110",
        "ts" : 1622298543910,
        "type" : "buy",
        "uid" : "6XaKYgXCsuMNg1d5bWYHg6ej5sd2"
      }
    }
  }
}

例如,当我执行函数 GetBuyList("EURUSD", 1.3) 或 GetBuyList("EURUSD", 1.1) 时,结果返回为:

{
  '-MasVPCtD4sdPCcdF9S9': {
    close_type: 'None',
    lot_size: 1,
    order_price: 1.211,
    sl: 'buy_sl_1.210',
    symbol: 'EURUSD',
    tp: 'buy_tp_1.23',
    ts: 1622298174339,
    type: 'buy',
    uid: '6XaKYgXCsuMNg1d5bWYHg6ej5sd2'
  }
}

当我执行 GetBuyList("BTCUSD", 34890) 这样的函数时,它会返回:

{
  '-Masii03kq9LvuLfWOyG': {
    close_type: 'None',
    lot_size: 1,
    order_price: 34888.17,
    sl: 'buy_sl_34887',
    symbol: 'BTCUSD',
    tp: 'buy_tp_34889',
    ts: 1622301925456,
    type: 'buy',
    uid: '6XaKYgXCsuMNg1d5bWYHg6ej5sd2'
  }
}

但是当我运行 GetBuyList("BTCUSD", 34886) 时,没有任何回报。

【问题讨论】:

  • 1) 您能否使用硬编码值显示不返回您期望结果的查询。 2) 您能否将您期望从该查询返回的 JSON 显示为文本,而不是屏幕截图?您可以通过单击Firebase Database console 的溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。
  • @FrankvanPuffelen 嗨弗兰克,感谢您的及时回复,我已编辑帖子以获取更多信息。

标签: javascript firebase firebase-realtime-database firebase-admin


【解决方案1】:

sltp 都是字符串,因为它们是字符串,所以它们不会被解析为数字,而是要进行字典排序。

发生这种情况的最常见示例之一是如果您查看文件夹中的文件列表:

0.jpg
1.jpg
10.jpg
11.jpg
12.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
9.jpg

如果你不能从使用字符串切换,你需要用你期望的最大数字填充数字:

000.jpg
001.jpg
002.jpg
003.jpg
004.jpg
005.jpg
006.jpg
007.jpg
008.jpg
009.jpg
010.jpg
011.jpg
012.jpg
const formatWithPadding = (inp, digits) => {
  let n = Number(inp), nStr = `${Math.abs(n)}`, sign = n<0;
  return (sign ? '+' : '-') + (
    nStr.length > digits
      ? nStr
      : `${"0".repeat((digits || 1) - 1)}${nStr}`.slice(-digits)
  )
};

const tpVal = 1.210;
const [integerPart, fractionalPart] = String(tpVal).split(".");
const tp = `buy_tp_${formatWithPadding(integerPart, 6)}.${fractionalPart || 0}`;

// tp is "buy_tp_+000001.210"

【讨论】:

  • 谢谢山姆!将我的查询 endAt 参数从 buy_sl_${100000000} 更改为 buy_sl_${999999999} 后,我的问题解决了,这是由于字典排序。赞赏!!
  • 顺便说一句,我真的应该填充数字而不是更改 endAt 值,因为它认为 2 大于 10。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多