未捕获(在承诺中)TypeError:无法读取未定义的属性“remove”

时间:2017-05-01 19:32:44

标签: javascript reactjs ecmascript-6 react-redux

我收到了奇怪的错误

另一个答案说它与不正确的进口有关。我试过声明一些方法,比如var render_product_url = function()....等等,都失败了。

AddRendering.js

import React from "react"


export function add_rendering(headers, target_header, rendering_callback) {
    var new_headers = [];
    for (let i = 0; i < headers.length; i++){
        var this_header = headers[i];
        if (this_header == target_header){
            var header_object = {
                title: target_header,
                render: rendering_callback
            };
            new_headers.push(header_object);
        } else {
            new_headers.push(this_header);
        }
    }
    return new_headers
}

function render_product_url(val, row) {
    return (<a href={"http://www.website.com/product/" + row[target_header]} target="_blank">{row[target_header]}</a>)
}

export function render_product_url_in_datatable(headers, target_header) {
    return add_rendering(
        headers,
        target_header,
        render_product_url
    )
}

ThisController.js

import React from "react"
import { render_product_url_in_datatable } from '../commonutils/AddRendering';


render() {
  ..
  these_metrics.headers = render_product_url_in_datatable(these_metrics.headers, 'Part Number');
  ..
}

最奇怪的部分是这样的:

import React from "react"


export function add_rendering(headers, target_header, rendering_callback) {
    var new_headers = [];
    for (let i = 0; i < headers.length; i++){
        var this_header = headers[i];
        if (this_header == target_header){
            var header_object = {
                title: target_header,
                render: rendering_callback
            };
            new_headers.push(header_object);
        } else {
            new_headers.push(this_header);
        }
    }
    return new_headers
}

// function render_product_url(val, row) {
//     return (<a href={"http://www.website.com/p/" + row[target_header]} target="_blank">{row[target_header]}</a>)
// }

export function render_product_url_in_datatable(headers, target_header) {
    return add_rendering(
        headers,
        target_header,
        function render_product_url(val, row) {
            return (<a href={"http://www.website.com/p/" + row[target_header]} target="_blank">{row[target_header]}</a>)
        }
    )
}

匿名也有效,比如

return add_rendering(
            headers,
            target_header,
            function(val, row) {
              ..
            }

与普通语言相比,我发现这种JS导入语法不实用,但这对JS来说是正常的。

为什么你不能在javascript中使用文件中其他地方声明的函数?这是使用ES6和React。谢谢

1 个答案:

答案 0 :(得分:1)

AddRendering.js上你要么没有发布整个代码(缺少某些导入或其他内容?)或者你忘了在target_header函数中包含render_product_url作为参数(那是什么) val param虽然?)。

function render_product_url(val, row) {
    return (<a href={"http://www.website.com/product/" + row[target_header]} target="_blank">{row[target_header]}</a>)
}

如果这是一个缺失的导入,你不确定如何import这是要走的路:

import target_header from './path/to/target_header'; // in case of a default export
import {target_header} from './path/to/target_header'; // in case of name export