How to access specific value in this 'json'? - 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.

Related

Property 'name' does not exist on type 'string'

I have a list of objects and I need to iterate over the list and add a certain property (string) from the object to a map of <string, boolean>. Here is my code:
this.selectedPeople.forEach(element => {
this.checkedPeople.set(element.name, true);
}
The purpose of this is to set checkboxes to be checked for certain people (if they are in the selectedPeople list) using [checked] in Angular. For some reason, I get this error when trying to build the project:
Property 'name' does not exist on type 'string'
I tried to change element.name to just element and that provides errors during compilation but when I look at the frontend, none of the checkboxes are checked (as you would expect since the key in the map is not a string as required). I have done similar things with other lists and they seem to be working fine, so why is this an issue here?
Edit1: I have seen some other solutions on here but they do not seem to be relevant to my case or have not worked.
Edit2: This is not the actual code as it is company code so I have tried to recreate the issue using a different scenario so as not to run into any confidentiality issues. To elaborate further, the selectedPeople array would be something like this:
[
{
"name":"Paul",
"age":24,
"sport":"Football"
},
{
"name":"Tom",
"age":22,
"sport":"Tennis"
}
]
Edit3: For further clarification, here is what my checkedPeople map looks like:
0: {"Paul" => false}
1: {"Jennifer" => false}
2: {"Georgia" => false}
3: {"Tom" => false}
And I am trying to change the value to true for each person who is in the selectedPeople array.
I tried using element['name'] instead of element.name and that seemed to do the trick. I do not quite understand why this works (especially given that element.name is used elsewhere and works fine) so if anyone has any idea, feel free to comment.
Without seeing your project setup, I'm betting that typescript thinks that element can be a string or an object... something like this:
type SelectedPeople = Array<string | { name: string }>
In such as case, you will need to make sure the item is not a string before accessing the name property:
this.selectedPeople.foreach(element => {
if(typeof element === 'string') {
this.checkedPeople.set(element, true);
} else {
this.checkedPeople.set(element.name, true);
}
}
Yes, you can either try what Ryan is suggesting above, or alternatively you can add a safe check before setting each element like so:
this.selectedPeople.forEach(element => {
If (element && element.Name){
this.checkedPeople.set(element.name, true);
}
}

Is there a way of getting a cleaner JSON out of Couchdb?

I am wondering if there is a way to get a cleaner JSON with just the docs out of Couchdb instead of getting it under "rows" and then "doc"
This is the default output
{
"total_rows":1,
"offset":0,
"rows":[
{
"id":"7d9fd5824f9029186c1eec1bda005e75",
"key":"7d9fd5824f9029186c1eec1bda005e75",
"value":{
"rev":"1-f99879f67cfd27685f92c884c236a0fd"
},
"doc":{
"_id":"7d9fd5824f9029186c1eec1bda005e75",
"_rev":"1-f99879f67cfd27685f92c884c236a0fd",
"title":"Hello World",
"messeges":"This is the first messege. Helloo there"
}
}
]
}
This is the desired output:
{
"_id":"7d9fd5824f9029186c1eec1bda005e75",
"_rev":"1-f99879f67cfd27685f92c884c236a0fd",
"title":"Hello World",
"messeges":"This is the first messege. Helloo there"
}
Thanks
It would be useful to see your code. I suspect this is the output of the alldocs api? If you know the ID of the document you want you can use the get api which returns the JSON you want. Otherwise you must loop through the "rows" ie
for (x in results.rows) {...}
and then use x.doc to get your JSON.
Thanks all for the help.
I figured out a way doing it in Node js by using a map function attached to there query
in the nano page they listed it as
alice.list().then((body) => {
body.rows.forEach((doc) => {
console.log(doc);
});
});
instead I used it like this
alice.list().then((body) => {
var result = body.rows.map( (x) => {
return x.doc
});
console.log(result);
});
which works for me.
Still new till couchdb and databases in general.

Should I convert JSON to Array to be able to iterate it with v-for?

In the following code I am getting data from server and filling array with them:
Vue.http.post('/dbdata', DataBody).then((response) => {
App.$refs.userContent.rasters_previews_list.$set(response); // putting JSON answer to Component data in userContent
console.log("App.$refs.userContent.rasters_previews_list: ", App.$refs.userContent.rasters_previews_list.length);
}, (response) => {
console.log("Error")
});
Now I am filling. data is declared in var userContent = Vue.extend({. I am using App.$refs.userContent.rasters_previews_list to set it's value, because one man from SO said that there is no other way to get access to constructor. I tried to do output of rasters_previews_list after changing with watch, but here is what I am see. http://img.ctrlv.in/img/16/08/04/57a326e39c1a4.png I really do not understand am I setting it's right way or no. If yes, why I do not see data and see only this crap?
data: function () {
return {
rasters_previews_list: []
}
}
But How I can iterate it with v-for?
<ul v-for="img in rasters_previews_list">
<li>{{img}}</li>
<ul>
This code is display one bullet. So it's look like it's assume that there is one object.
My object in browser console look like:
Object {request: Object, data: Array[10], status: 200, statusText: "OK", ok: true}
Your setting the full response instead of just the data you actually need.
Vue.http.post('/dbdata', DataBody).then((response) => {
App.$refs.userContent.rasters_previews_list.$set(response.data);
console.log("App.$refs.userContent.rasters_previews_list: ", App.$refs.userContent.rasters_previews_list.length);
}, (response) => {
console.log("Error")
});
If this isn't what you are looking for please post a full example.

Using Json in KRL

I'm having trouble with parsing my Json, when i place the url in the browser i get this as a return {"token": "7xv6r32eay5n376", "secret": "589bc72ix7mowua"} So all i want to do is get that string and parse out the token and secret and display the values in a notify to confirm i'm getting the correct information. Can anyone see what i'm doing wrong?
rule first_rule {
select when pageview ".*" setting ()
pre{
json=http:get(/* I place my URL here */);
content = json.pick("$..content");
token=content.decode();
tok=token.pick("$..token");
sec=token.pick("$..secret");
message="Token: "+tok+" "+"Secret: "+sec;
}
notify("Values: ",message);
}
}
so i fixed my KRL problem, I guess when using http:get(); you must use double quotes "" not single '' in the get().

Defining recrussive JSON Object notation

Whats wrong in below JSON Object definition
I am trying to create a new JSON Object like below.
So that my idea is to access COMPANYSETUP.HourSetup.initiate();
Not sure the error ?
COMPANYSETUP = {
initiated : false,
companyId : "",
initiate : function() {
if(!initiated){
// TO DO
initiated = true;
} },
HourSetup = {
initiated : false,
hourId : "",
initiate : function() {
if(!initiated){
// TO DO
initiated = true;
}
}
}
};
Assuming you want a javascript object and not JSON, which disallows functions,
HourSetup =
Should be changed to:
HourSetup :
Also, as JonoW points out, your single line comments are including some of your code as the code is formatted in the post.
There's a "=" that shouldn't be there. Change it to ":"
JSON is a form of JavaScript deliberately restricted for safety. It cannot include dangerous code elements like function expressions.
It should work OK in plain eval()ed JavaScript, but it's not JSON.