尝试从数组对象读取时无法读取未定义的属性

时间:2020-03-04 11:55:27

标签: angular jasmine

我正在用角度8编写茉莉花测试,但遇到错误无法读取undefined属性。它试图读取数组对象的属性。我已经尝试过初始化,但是似乎没有用。

我正在尝试初始化component.myData

测试

fit('should update  when acceptSection is called ', () => {
   let updateSpy: jasmine.Spy;
   setupComponent();

   component.myData = [] = ['reviewWindowExpiry'];

   updateSpy = spyOn(component, 'update').withArgs(3).and.returnValue(true);
   component.acceptSection(true);
   expect(updateSpy).toHaveBeenCalled();
});

组件

myData: any;

getNextSectionContent(contentIndex: number) {
    // Get scroll height of agreement
    this.agreementScrollHeight = this.scroll.nativeElement.scrollHeight;

    // Calculate remaining days left on next agreement
    this.hoursUntil = null; // reset this value
    this.daysUntil = differenceInDays(this.myData[contentIndex].reviewWindowExpiry, this.todaysDate);

    if (this.daysUntil < 1) {
      this.hoursUntil = differenceInHours(this.myData[contentIndex].reviewWindowExpiry, this.todaysDate);
      this.daysUntil = null;
    }

    if (this.myData[contentIndex] !== undefined) {
      this.agreementData = this.myData[contentIndex].data;
      this.scroll.nativeElement.scrollTop -= this.agreementScrollHeight;
    } else {
      this.agreementData = 'NO MORE AGREEMENTS!!';
    }
  }

1 个答案:

答案 0 :(得分:0)

您正在将.withArgs(3)传递给更新函数。

设置或影响contentIndex的值吗? (例如,可能使数组循环3次)。

看起来好像试图检索不在数组中的索引的数组条目,因为您只向其中添加了一项。

这能给您带来价值吗?

if (isset($_POST['submit'])) {
    $DATE = $_POST['date'];
    $PRODUCT_ID = $_POST['product_id'];
    $BRANCH_ID = $_POST['branch_id'];
    $PROD_PURCHASE_PRICE = $_POST['product_unit'];
    $QUANTITY = $_POST['quantity'];
    $sql = "SELECT * FROM `inventory_master` WHERE `PRODUCT_ID` = '$PRODUCT_ID' AND `BRANCH_ID` = '$BRANCH_ID'";
    $inventory_distribution_details = $conn->query($sql)->fetch_assoc();
    if(isset($inventory_distribution_details)){
        if($inventory_distribution_details['QUANTITY'] >= $QUANTITY){
            $sql2 = "INSERT INTO inventory_distribution(DATE,PRODUCT_ID,BRANCH_ID,PROD_PURCHASE_PRICE,QUANTITY) VALUES('$DATE','$PRODUCT_ID','$BRANCH_ID','$PROD_PURCHASE_PRICE','$QUANTITY')";
            $result2 = $conn->query($sql2);
            if ($result2 === TRUE) {
                $last_id = $conn->insert_id;
            }else{
                $error = array('title' => "Error occured. Please try again.", 'icon' => "warning");
            }
        }else{
            $error = array('title' => "Quantity should be not be greater than ".$inventory_distribution_details['QUANTITY'], 'icon' => "warning");
        }
    }else{
        $error = array('title' => "Product not available in Inventory", 'icon' => "warning");
    }
}

还认为Date.UTC将返回一个函数,请尝试

//contentIndex value is 0
this.myData[0].reviewWindowExpiry

获取数字毫秒值。