2017-08-17 179 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

相关问题