【问题标题】:Firebase basics - JavaScript and forms validationFirebase 基础 - JavaScript 和表单验证
【发布时间】:2016-02-17 11:38:25
【问题描述】:

我想使用 Firebase 创建优惠券代码数据库。我的数据结构如下:

[
    { 
        "couponCode": "string",
        "isAvailable": true
    },
    {
        "couponCode": "string",
        "isAvailable": true
    },
    ...
]

我不会得到couponCode 键值和isAvailable 键的下一个检查值。如果couponCode 有效,我想将isAvailable 键的值更改为false。如何使用 Firebase API 创建此验证?

【问题讨论】:

  • 在下面回答。但请在继续您的道路之前阅读Firebase documentation。在快速入门和指南中花费几个小时将避免将来出现许多问题。

标签: javascript arrays json validation firebase


【解决方案1】:

如果优惠券代码是唯一的,您最好像这样存储数据:

"couponCodes": {
  "couponCode1": {
    "available": true
  },
  "couponCode2": {
    "available": true
  }
}

现在,如果用户尝试领取优惠券,您可以在该代码上运行 transaction

ref.child('couponCodes').child('couponCode1').transaction(function(current) {
  if (current && current.available) {
    current.available = false;
    // TODO: this is the moment that you'll also want to give the user the discount for the coupon
  }
  return current;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    相关资源
    最近更新 更多