Best answer

Got a result missing the "id" property

  • 2 April 2021
  • 1 reply
  • 36 views

Userlevel 1
const timestart =Math.floor(Date.now() / 1000) -  bundle.inputData.startTimestamp;
    const endtime = Math.floor(Date.now() / 1000) - bundle.inputData.endTimestamp
const options = {
  url: `${bundle.authData.server}/org/${bundle.authData.orgId}/contacts`,
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Authorization': `Token ${bundle.authData.token}`
  },
  params: {
    'startTimestamp': timestart,
    'endTimestamp': endtime,
    'reportType': bundle.inputData.reportType,
    'fields': bundle.inputData.fields
  }
}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = response.json;

    // You can do any parsing you need for results here before returning them

    return results;
  });

here is the code that I am executing. When I execute I get the following errors 

 

CheckError: Invalid API Response: - Results must be an array, got: object, ({"50219":{"campaigns":[{"campaignId":63651,"campai) - Got a result missing the "id" property ({"campaigns":[{"campaignId":63651,"campaignName":"Activation Flow"}],"fields":{"name":"Chris Collins"}})

 

Here is the actual JSON that is returned from the API. 

 

{
"547012": {
"campaigns": [
{
"campaignId": 63651,
"campaignName": "Activation Flow"
}
],
"fields": {
"name": "Paul Collins"
}
},
"547013": {
"campaigns": [
{
"campaignId": 63651,
"campaignName": "Activation Flow"
}
],
"fields": {
"name": "jenny Collins"
}
}
}

the value 547012 and 547013 should be the id but I do not know how to turn the response into that?

icon

Best answer by ikbelkirasan 2 April 2021, 22:49

@cozza13 - Try the following code snippet:

const timestart =
Math.floor(Date.now() / 1000) - bundle.inputData.startTimestamp;
const endtime = Math.floor(Date.now() / 1000) - bundle.inputData.endTimestamp;

const options = {
url: `${bundle.authData.server}/org/${bundle.authData.orgId}/contacts`,
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Token ${bundle.authData.token}`,
},
params: {
startTimestamp: timestart,
endTimestamp: endtime,
reportType: bundle.inputData.reportType,
fields: bundle.inputData.fields,
},
};

return z.request(options).then((response) => {
response.throwForStatus();
const results = Object.entries(response.json).map(([key, value]) => {
return Object.assign({}, value, {
id: key,
});
});
return results;
});

 

View original

1 reply

Userlevel 7
Badge +11

@cozza13 - Try the following code snippet:

const timestart =
Math.floor(Date.now() / 1000) - bundle.inputData.startTimestamp;
const endtime = Math.floor(Date.now() / 1000) - bundle.inputData.endTimestamp;

const options = {
url: `${bundle.authData.server}/org/${bundle.authData.orgId}/contacts`,
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Token ${bundle.authData.token}`,
},
params: {
startTimestamp: timestart,
endTimestamp: endtime,
reportType: bundle.inputData.reportType,
fields: bundle.inputData.fields,
},
};

return z.request(options).then((response) => {
response.throwForStatus();
const results = Object.entries(response.json).map(([key, value]) => {
return Object.assign({}, value, {
id: key,
});
});
return results;
});

 

Reply