admin管理员组

文章数量:1400844

This is frustrating. I thought the problem was the object being returned by the api response. Maybe it's in string so what I did was I copied the response from "postman" and paste it directly on the js. This way im sure that it's in object/array. But the result was the same error.

Why is my code not working on netsuite. The code below is very simple. Tried running it on my local machine and it worked. Does .find not supported in netsuite?

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>

This is frustrating. I thought the problem was the object being returned by the api response. Maybe it's in string so what I did was I copied the response from "postman" and paste it directly on the js. This way im sure that it's in object/array. But the result was the same error.

Why is my code not working on netsuite. The code below is very simple. Tried running it on my local machine and it worked. Does .find not supported in netsuite?

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>

Any help is greatly appreciated.

Share Improve this question asked Dec 21, 2017 at 14:05 Wondering CoderWondering Coder 1,70211 gold badges31 silver badges51 bronze badges 4
  • This should work. If find wasn’t supported, reduce should’ve caused an error before find. And find is always used on an array, as far as I can tell. – Sebastian Simon Commented Dec 21, 2017 at 14:12
  • 2 Not sure what netsuite runs, but maybe the engine it uses does not support it. Maybe add the polyfill developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – epascarello Commented Dec 21, 2017 at 14:13
  • I haven't done any polyfill before. – Wondering Coder Commented Dec 21, 2017 at 14:33
  • @epascarello, it worked. I added a polyfill for find. thanks mate – Wondering Coder Commented Dec 21, 2017 at 14:40
Add a ment  | 

1 Answer 1

Reset to default 5

I see you added a polyfill to solve the problem, but the answer to your question is that NetSuite/SuiteScript uses ECMAScript 5.1; its engine is Java Rhino (unsure which version).

find was added in ES6, while reduce was added in ES5.1, so SuiteScript does not support find while it does support reduce.

Sources:

  • Reduce: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Specifications
  • Find: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Find#Specifications

本文标签: javascriptTypeError Cannot find function find in objectStack Overflow