Jscript help from the pros

Hey guys I need a little help from some scripting pros.

function getuser(pincode) {
  var getusers = web.PostData('https://api.7shifts.com/v1/'+method2+'/','',''+apikey+'','');
  var user = JSON.parse(getusers);
  for(var i=0; i<user.data.length; i++){
   if(user.data[i].user.employee_id == pincode)
   return user.data[i].user.id;

This function is valid… the problem is the data is so huge if it tries to search for something that is way way way down near end it returns undefined. Im guessing its timing out?

Any ideas on how to improve this?

In this case the restaurant has a LOT of employees if I search for an employee that is near end of the list it returns undefined… if I search for one near the start it works fine.

Hmm, is there an endpoint of that API where you can specify who are you looking for? Or do you really need the whole list every time?

I’m wondering if it’s a timeout issue with WebClient. But were that the case I’d expect malformed JSON and JSON.parse() should fail.

web.PostData() should be returning the complete response and not a stream.

Are you able to dump getusers to a file and see if its complete?

Is it possible 7shifts isn’t returning the complete list of employees?

It does return compete data. I had a second return to test it.

Is it timing out when it getting the string or is it timing out when its parsing it?

When it’s getting the string. I think memo is right. I think the helper is timing out. There are about 500 employees.

Is that an actual list of users from an existing business you are trying to integrate or is it just a demo where anyone can add users and test it?

Just asking if its just a demo maybe someone left some incomplete data or something along the line.

It’s real. Live data. It’s a large high volume chain.

If you return user.data.length before the for loop does that give you the employee count you expect?

Hmm I will try that.

How bit is object for a user?
I had some pretty big json arrays in my pms int.

It definatly working? Method and api key coming from wherever corect?

Posibly declare length to a variable then used in the for to, unlikely but maybe to be sure.

Presumeably employee_id isn’t a field you can request an employee data for?

What as the seccond return can I ask?

Try this for kicks

function getuser(pincode) {
  var getusers = web.PostData('https://api.7shifts.com/v1/'+method2+'/','',''+apikey+'','');
  var userList = JSON.parse(getusers);
  for (var i = userList.data.length; i > -1; i--) {
   if (userList.data[i].user.employee_id == pincode) return userList.data[i].user.id;
1 Like

You should also verify if the response is correctly formated (http://jsonviewer.stack.hu/) any error in the json would make to fail the script, since samba scripting engine cannot throw any exception its harder to guess whats happening, have you tried to run that code in javascript to see if is samba problem? I don’t think 500 entries are too large to fail.