TypeError:找不到对象

时间:2017-12-21 14:05:38

标签: javascript netsuite suitescript2.0

这令人沮丧。我认为问题是api响应返回的对象。也许它是字符串所以我做的是我复制了“postman”的响应并将其直接粘贴到js上。这样我确定它在对象/数组中。但结果是同样的错误。

为什么我的代码不能在netsuite上运行。下面的代码非常简单。尝试在我的本地机器上运行它,它工作。在netsuite中不支持.find吗?

var tasks_data = new Array();
  var tasks_data = [
    {
      'id': 10376401,
      'name': 'closed',
      'notes': null,
      'start_date': '2017-12-23',
      'end_date': '2018-01-07',
      'start_time': null,
      'end_time': null,
      'color': '#f99bd0',
      'color_id': 5,
      'estimated_hours': 0,
      'done': false,
      'user_id': 961775,
      'project_id': null,
      'project': null,
      'folder_id': null,
      'weight': 0,
      'created_at': '2017-11-13T00:58:16.577+00:00',
      'updated_at': '2017-11-13T00:58:16.577+00:00',
      'deleted_at': null
    },
    {
      'id': 10438883,
      'name': '',
      'notes': null,
      'start_date': '2018-02-17',
      'end_date': '2018-02-17',
      'start_time': null,
      'end_time': null,
      'color': '#ccaf53',
      'color_id': 36,
      'estimated_hours': 0,
      'done': false,
      'user_id': 961775,
      'project_id': 1501267,
      'project': {
        'id': 1501267,
        'name': 'sue',
        'color': '#ccaf53',
        'color_id': 36,
        'client': null,
        'created_at': '2017-11-17T03:14:11.459+00:00',
        'updated_at': '2017-11-17T03:14:11.459+00:00'
      },
      'folder_id': null,
      'weight': 0,
      'created_at': '2017-11-17T03:15:48.055+00:00',
      'updated_at': '2017-11-17T03:15:48.055+00:00',
      'deleted_at': null
    },
    {
      'id': 10438875,
      'name': 'Sue',
      'notes': null,
      'start_date': '2018-01-27',
      'end_date': '2018-01-27',
      'start_time': null,
      'end_time': null,
      'color': '#ccaf53',
      'color_id': 36,
      'estimated_hours': 0,
      'done': false,
      'user_id': 961775,
      'project_id': 1501267,
      'project': {
        'id': 1501267,
        'name': 'sue',
        'color': '#ccaf53',
        'color_id': 36,
        'client': null,
        'created_at': '2017-11-17T03:14:11.459+00:00',
        'updated_at': '2017-11-17T03:14:11.459+00:00'
      },
      'folder_id': null,
      'weight': 0,
      'created_at': '2017-11-17T03:14:11.903+00:00',
      'updated_at': '2017-11-17T03:14:50.363+00:00',
      'deleted_at': null
    }
  ];

  // var result = output_result(tasks_data)
  var result = tasks_data.reduce(function (acc, item) {
    var task = acc.find(function (accItem) {
      return accItem.project_id === item.project_id
    })
    if (task && !Array.isArray(task.schedule)) {
      task.schedule = [task.schedule].concat({
        project_id: item.project_id,
        start_date: item.start_date,
        end_date: item.end_date,
        daily_estimate: item.estimated_hours,
      })
      task.start_dates.push(item.start_date)
      task.end_dates.push(item.end_date)
      task.next_start_dates.push(item.start_date)
    } else if (task && Array.isArray(task.schedule)) {
      task.schedule.push({
        project_id: item.project_id,
        start_date: item.start_date,
        end_date: item.end_date,
        daily_estimate: item.estimated_hours,
      })
      task.start_dates.push(item.start_date)
      task.end_dates.push(item.end_date)
      task.next_start_dates.push(item.start_date)
    } else {
      acc.push({
        project_id: item.project_id, //(item.project_id === null) ? 'Missing_project_id' : item.project_id,
        schedule: [{
          project_id: item.project_id, //(item.project_id === null) ? 'Missing_project_id' : item.project_id,
          start_date: item.start_date,
          end_date: item.end_date,
          daily_estimate: item.estimated_hours,
        }],
        start_dates: [item.start_date],
        end_dates: [item.end_date],
        next_start_dates: [item.start_date],
      })
    }

    return acc
  }, [])
  
  console.log(result)
<body>
Hello
</body>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

我看到你添加了一个polyfill来解决问题,但问题的答案是NetSuite / SuiteScript使用 ECMAScript 5.1 ;它的引擎是Java Rhino(不确定哪个版本)。

ES6中添加了

find,而ES5.1中添加了reduce,因此SuiteScript不支持find,但支持reduce

来源: