2017-08-17 60 views
1

我的render方法內部我調用一個方法,它根據API響應數據執行一些條件邏輯。我不清楚如何解決這個錯誤,考慮到我沒有從DOM中刪除任何東西。這裏是我的方法TypeError:無法在'Node'上執行'removeChild':參數1在React中不是'Node'類型

import numeral from 'numeral'; 
import moment from 'moment'; 

class SearchResultsListItem extends React.Component { 
    constructor(props) { 
    this.isMaxQtyGreaterThanOneThousand= 
    ::this.isMaxQtyGreaterThanOneThousand; 
    } 

isMaxQtyGreaterThanOneThousand(qty){ 
    if(!!qty){ 
    if(qty%1000 === 0){ 
     return (numeral(qty).divide(1000).format('0,000') + 'M'); 
    }else{ 
     return numeral(qty); 
    } 
    }else{ return "-";} 
} 


render() { 
    const x = this.props.dataItem; 

    return (
    <div className={`${s['quantity-block']}`}> 
     {this.isMaxQtyGreaterThanOneThousand(x.MaxQuantity)} 
    </div> 
) 
} 

我看,以解決此錯誤下面貼了,但沒有多少運氣找到任何特定於JSX或陣營:

Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

回答

1

錯誤是因爲在某些情況下,您返回numeral對象而不是輔助方法中的文本字符串。這是令人困惑的React。

你可能想

return numeral(qty).format('0,000'); 

,而不是

return numeral(qty) 
+0

我執行我的'render'方法之外對我的評價,像這樣: 常量adjustedMaxQty =數字(x.MaxQuantity).divide( 1000).format('0,000')+'M'; 然後在我的渲染方法中,我執行以下邏輯: {x.MaxQuantity? ((x.MaxQuantity> 1000)?adjustedMaxQty:x.MaxQuantity) : 「 - 」 } 這解決了我的錯誤。 – eagercoder

相關問題