【问题标题】:Ionic prevent add data if firebase data is 0如果 Firebase 数据为 0,Ionic 会阻止添加数据
【发布时间】:2018-02-15 06:06:05
【问题描述】:

我正在制作 Ionic 库存库存应用程序。在我的可用股票页面中,我正在跟踪这样的用户数据:

<ion-item *ngFor="let stock of stocks | async" class="item item-trns text-center"
    (click)="showOptions(stock, stock.$key)">
      <strong><h2>{{ stock.name }}</h2></strong>
      <p>Date added: {{stock.date}}</p>
      <p>Quantity in stock: {{stock.actualq-stock.sold}}</p>
      <p>Sold: {{stock.sold}}</p>

这显示了我的 Firebase 后端中所有股票的列表。

showOptions(stock, stock.$key)

提供出售特定物品的选项。

如您所见,库存剩余数量计算为

Quantity in stock: {{stock.actualq-stock.sold}}

即实际数量减去已售出的数量。

我的问题: 添加销售额时,如何防止用户添加大于现有库存数量的销售额?这将导致库存的剩余数量为负值。

我尝试使用表单验证,但我不知道如何检查输入的数据是否大于我的 Firebase 后端中可用的库存。

如果 {{stock.actualq-stock.sold}} 为 0,有没有办法禁用列表选项?

这是我的卖出函数:

addSale(id,saledate,salequantity,buyingprice,sellingprice,sale,profit,saletotal) {
    if(id) {
      this.stocks.update(id, {
        saledate: saledate,
        salequantity: salequantity,
        buyingprice: buyingprice,
        sellingprice: sellingprice,
        sale: true,
        profit: (sellingprice-buyingprice)*salequantity,
        saletotal: (salequantity*sellingprice),

      }).then( newSale => {
            this.toast.show('Data updated', '5000', 'center').subscribe(
              toast => {
                this.navCtrl.pop();
              }
            );
          })
          .catch(e => {
            console.log(e);
            this.toast.show(e, '5000', 'center').subscribe(
              toast => {
                console.log(toast);
              }
            );
          });

所做的只是更新正在销售的特定库存商品的路径,并添加上面的节点以反映该商品已添加销售。

【问题讨论】:

  • 你能发布你的销售功能吗?
  • @Hareesh 我已经编辑了问题以包含销售功能
  • 你在哪里调用这个函数addSale()在模板中?。不是在ion-item里面吗?
  • @Hareesh 我通过这样的按钮在模板中调用它:&lt;button id="addSale-button" ion-button clear color="balanced" block large (click)="addSale(product.id, product.saledate, product.salequantity, product.buyingprice, product.sellingprice, product.sale)" [disabled]="!saleForm.valid"&gt; &lt;ion-icon name="add-circle"&gt;&lt;/ion-icon&gt;&lt;/button&gt;

标签: node.js firebase ionic3 angularfire2


【解决方案1】:

如果我理解正确,您需要更新股票 ID。因此,在更新之前获取当前数据并检查手头的库存并执行更新。试试

addSale(id,saledate,salequantity,buyingprice,sellingprice,sale,profit,saletotal) {
    if(id) {
        this.db.object('stocks/'+id).snapshotChanges().subscribe(action => {
           console.log(action.payload.val());
           let stockInHand = action.payload.val().actualq-action.payload.val().sold;
           //check if salequantity is lessthan or equal to stockInHand
           if(salequantity <= stockInHand){
               //perform update
           }else{
               //toast no stock available 
           }
        });
    }
}

【讨论】:

  • 谢谢。这种方法很有意义,似乎可行。但是当我用afoDatabase.object 替换db.object 时,我正在使用angularfire2-offline 并且在类型'AfoObjectObservable' 上不存在错误'snapshotChanges'。如果我将其保留为 db.object,则会收到错误“属性 'db' does not exit on type 'myPage'。
  • 即使在切换到使用 af2 之后,我仍然收到错误 snapshotChanges() does not exist on type FirebaseObjectObservable
  • 我不确定angularfire2-offline。这里db 我的意思是constructor(db: AngularFireDatabase)。它将与FirebaseObjectObservable 一起使用link。如果不是,可能是您在 af2 上使用的是旧版本,请指定您的版本
  • 能够通过一些更改使其工作。谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-04
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多