TypeError:无法在'Node'上执行'removeChild':参数1在React中不是'Node'类型

时间:2017-08-17 16:57:39

标签: javascript reactjs conditional jsx

在我的render方法中,我正在调用一个基于API响应数据执行一些条件逻辑的方法。我不清楚如何解决这个错误,考虑到我没有从DOM中删除任何东西。这是我的方法

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或React特有的任何内容都没有多少运气:

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 个答案:

答案 0 :(得分:1)

错误是因为在某些情况下,您正在从助手方法返回numeral对象而不是文本字符串。这让React感到困惑。

你可能想要

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

而不是

return numeral(qty)