Read single second level JSON entry with Ionic - json

I've got this JSON file that I'm trying to parse the second level off. But it seems to fail.
{
"error":false,
"accessToken":"xxx",
"accountId":"xxx",
"account":[
{
"error":false,
"id":"2",,
"username":"Username"
}]
}
Here's the Ionic code
return $http.post(SERVER.apiUrl + SERVER.apiLogin,
{username: "username",password: "password"})
.success(function(data){
o.setSession(data.accountId, data.accessToken, data.account[0]["username"]);
});
Error
TypeError: Cannot read property '0' of undefined
at services.js:103
at ionic.bundle.js:17151
at processQueue (ionic.bundle.js:20962)
at ionic.bundle.js:20978
at Scope.$eval (ionic.bundle.js:22178)
at Scope.$digest (ionic.bundle.js:21994)
at Scope.$apply (ionic.bundle.js:22282)
at done (ionic.bundle.js:17439)
at completeRequest (ionic.bundle.js:17629)
at XMLHttpRequest.requestLoaded (ionic.bundle.js:17570)
i have also tried with
data.account.username
and
data.account[0].username
Can anybody give a quick advice?
Best regards

do not understand why it does not work for you, I tried it and does not give me problems.
Although at least here, you put two commas after ID
data.account[0]["username"] Is Ok
 
{
"error":false,
"accessToken":"xxx",
"accountId":"xxx",
"account":[
{
"error":false,
"id":"2",, <-- ERROR
"username":"Username"
}]
}

Related

Why am I getting 'Error: Error serializing ___ returned from getStaticProps'?

I am receiving the following error when I call inside getStaticProps and I cannot figure out why:
Error: Error serializing `.lingo` returned from `getStaticProps` in "/".
Reason: `undefined` cannot be serialized as JSON.
I've placed the full app code on CodeSandbox. It won't be able to access the API but it does show where things are defined.
When I run the following query on GraphQL playground I get the expected response:
query {
allTerms {
id
term
slug
lead
}
}
You can see that this query is contained in lingo.service.js in the modules/lingo/services directory on the sandbox but the homepage has the Error serializing error. Is my function export async function getAll() not correct or am I calling it wrong in getStaticProps?
await getAll() is most likely returning undefined which is not serializable JSON. Defaulting to null would be one way to solve the issue.
export async function getStaticProps(context) {
return {
props: { lingo: (await getAll()) ?? null },
};
}
Right, this is supposed to be more of a comment but apparently I don't have enough reputation points to comment. So, I'll answer it like this.
Just check if your props (under getStaticProps()) are named correctly i.e. how they're named in the .json file you're trying to read. I ran into this issue because of a typo I had and just fixed it.

AWS-SDK issue w/ deleteMessageBatch, telling me MissingParameter but I'm not

I'm getting the following message
UnhandledPromiseRejectionWarning: MissingParameter: The request must contain the parameter DeleteMessageBatchRequestEntry.1.Id.
I think I'm following the documentation to a T at AWS-SDK/SQS
I'm using this code
var params = {
Entries: _.map(_.uniqWith(data.Messages,d=>d.MessageId),d=>({
Id: d.MessageId,
ReceiptHandle: d.ReceiptHandle
})),
QueueUrl: xx.QueueUrl
};
await sqs.deleteMessageBatch(params).promise();
This is what params looks like at the time of sending; looks just like the docs if you ask me...
{
Entries: [
{
Id: "83ba1e18-someid",
ReceiptHandle: "AQEB79CDI1Q+blablabla"
}
]
QueueUrl: "https://sqs.us-west-2.amazonaws.com/somequeeuurl"
}
My system:
aws-sdk: "^2.354.0",
MacOS - current
node 8.12.0
UnhandledPromiseRejectionWarning: MissingParameter: The request must contain the parameter DeleteMessageBatchRequestEntry.1.Id.
I just spent a long time looking at this error and debugging my code. What I finally figured out is that it seems to be trying to say that there needs to be at least one DeleteMessageBatchRequestEntry element in the request – there can't be 0. When I refactored our code and added a check to make sure that we wouldn't make a request if there were no entries in the list, this problem went away.
Is it possible that you are actually sending the following in certain situations?
{
Entries: []
QueueUrl: "https://sqs.us-west-2.amazonaws.com/somequeeuurl"
}

Error: Converting circular structure to JSON

I have a problem with my application. Anyone can help me?
Error:
Converting circular structure to JSON
My Service to create items and save on localstorage:
addItem(item: Item): void {
this.itens.unshift(item);
let itens;
if (localStorage.getItem('itens') == null){
itens = [];
itens.unshift(itens);
localStorage.setItem('itens', JSON.stringify(itens));
} else {
JSON.parse(localStorage.getItem('itens'));
itens.unshift(itens);
localStorage.setItem('itens', JSON.stringify(itens));
}
}
And my component.ts:
addItem(): void {
this.itemAdicionado.emit({
nome: this.nome,
unidade: this.unidade,
quantidade: this.quantidade,
preco: this.preco,
perecivel: true,
validade: this.validade,
fabricacao: this.fabricacao,
});
this.nome = '';
this.unidade ;
this.quantidade ;
this.preco;
this.validade;
this.fabricacao;
console.log(this.nome, this.unidade, this.quantidade, this.preco, this.validade, this.fabricacao);
}
This isn't an Angular error. It's a JavaScript runtime error thrown by the JSON.stringify function. The error tells you that itens contains a circular object reference. This is OK while you run the application, but when stringifying it causes a problem: the JSON generated would become infinitely long.
As Kevin Koelzer indicated in his answer. The problem is that you wrote itens.unshift(itens);. Basically this adds the array of items to the array of items, thus creating a circular reference. Therefore, writing itens.unshift(item); instead solves your problem and is probably what you intended to do anyway.
itens.unshift(itens);
could this be:
itens.unshift(iten);

How to access specific value in this 'json'?

I'm accessing an URL like http://localhost/wp-admin/admin-ajax.php?action=process
and it returns something like this :
{
"status": "processing",
"icon": "test"
"testing": 0
}
I've tried everything to get the status but it never works. I precise that it is an Ionic app.
Here is my code (in console.log, everything that I've tried):
process() {
let url: any = 'http://localhost/wp-admin/admin-ajax.php?action=process'
this.httpIonic.get(url, null, null)
.then(response => {
console.log(response.data);
console.log(response.data[0].status)
console.log(response.data[0]['status'])
console.log(response.data.data.status)
console.log(response.data.status)
console.log(response[0]['status']);
console.log(response[0].status)
console.log(response[0])
console.log(response.status)
})
}
It always returns either nothing, either everything.
What am I doing wrong ?
Thanks
standing their documentation ‘response.status’ should be the right one... I would add the catch so to get more info if something’s going wrong;
furthermore I would even try to replace both null with empty objects
After getting the response. put that value in in a variable. after that try to access it. it will work.
example
this.val = result;
this.MediaList=this.val.problemActionList;
value.MediaList=this.MediaList.MediaList;
Try using JSON.parse(response) and then refer to response.status.

Ignore Undefined objects in array of arrays variable

In Google Apps Script, I'm pulling data from an API. In the code below, the "output" variable contains an array of arrays. There is always at least one ["Response"] object, but sometimes there are 2.
My problem is, the code below isn't returning the second object (["Response"][1]) when it is present. I tried removing the "if" statement, but I get an error saying "TypeError: Cannot read property "Product" from undefined".
Does anyone know how to get the second object when it's present and ignore it when it's not?
var data = reportAPI();
var applications = data["applications"];
var output = []
applications.forEach(function(elem,i) {
output.push(["ID",elem["Id"]]);
output.push([elem["Response"][0]["Product"],elem["Response"][0]["Status"]]);
if (["Response"][1] != null) {
output.push([elem["Response"][1]["Product"],elem["Response"][1]["Status"]]);
}
}
P.S. I would even be happy with replacing the undefined object with "", but I'm not sure how to do it.
How about this modification? Please think of this as one of several answers. I used forEach to elem["Response"]. By this, values can be pushed by the number of elem["Response"].
From :
applications.forEach(function(elem,i) {
output.push(["ID",elem["Id"]]);
output.push([elem["Response"][0]["Product"],elem["Response"][0]["Status"]]);
if (["Response"][1] != null) {
output.push([elem["Response"][1]["Product"],elem["Response"][1]["Status"]]);
}
}
To :
applications.forEach(function(elem) {
output.push(["ID",elem["Id"]]);
elem["Response"].forEach(function(elem2) {
output.push([elem2["Product"],elem2["Status"]]);
});
});
If this didn't work, please tell me. I would like to modify.
The example below helps to account for the cases where the Response[0] or Reponse[1] are not "undefined" or "null". Putting !() will turn the Boolean values for "undefined" or "null" to true.
applications.forEach(function(elem,i) {
output.push(["ID",elem.Id]);
if(!(elem.Reponse[0]))
output.push([elem.Response[0].Product,elem.Response[0].Status]);
if (!(elem.Response[1])) {
output.push([elem.Response[1].Product,elem.Response[1]Status]);
}
}