Hello my problem is that i get undefined , when trying to accsess the array length, but everything works fine when i try to access only the array.
this above do not works ->
console.log(this.ref_number_response[0].info.length);
This works ->
console.log(this.ref_number_response);
and this is the whole
check_ref_number: function () {
this.ref_number_response = [];
axios.get('/is_referenceNumber_free/'+this.ref_number)
.then(response => this.ref_number_response.push({
info: response.data
}));
console.log(this.ref_number_response[0].info.length);
Event.$emit('reference_added', this.ref_number_response);
},
Emit the event after you recieve the data:
check_ref_number: function () {
axios.get('/is_referenceNumber_free/'+this.ref_number)
.then(response => Event.$emit('reference_added',[{info:response.data}]));
}
The problem is that you are getting the data asynchronously, and trying to use the data before it is ready.
You said you are trying to access the the array length, but
this.ref_number_response
is the array, so the only way this console.log(this.ref_number_response[0].info.length); is going to work ( you're trying to get the info property from first element of the array length not the actual array length ) is if info were an array as well. So you'd probably need to do something like:
console.log(this.ref_number_response.length);
Related
Currently I am trying to create a hacker news clone (not from the example given on website). Currently I made an api call that returns an array but I can't seem to get rid of the square brackets. For reference my code is below
onMount(() => {
fetch('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty&limitToFirst=10&orderBy="$key"')
.then((res) => {
return res.text();
})
.then((text) => {
items = text.split(",");
setTimeout(3000);
data = items.filter((val) => {
return val.replace(/[\[\]']+/g, "");
});
});
//console.log(data);
//getData(items).then(console.log);
});
Thanks in advance!
The API provides a JSON object, but you read it as text (res.text()). Replace this with res.json() and the result will automatically be parsed to an array of IDs.
There is no need to manipulate JSON in string form, just parse it/let it be parsed.
I have array of data (selected items) and I need to extract ids of this array into new array so I can send those ids only to back-end.
Code
method
toggleSelection(rows) {
console.log('this.multipleSelection : ',this.multipleSelection); // prints my default array (include all data)
this.multipleSelection.forEach(row => {
console.log('rows: ', row) // get each object of array (extract ids here)
// send axios request to backend (ids only)
});
},
Screenshot
here is result of console codes above
any idea?
At first I need to say I never worked with Vue.js. But in simple vanilla-javascript you could use the map function. I don't know if this works but Here is a possible answer:
yourids = this.multipleSelection.map(row => row.id);
I finally managed to write a raw INSERT-query with sequelize/Apollo/ExpressJS and it returns a json like
{"data":{"createActie":"[[{\"id\":1598}],1]"}}.
I can get to [[{\"id\":1598}],1] by
await this.$apollo
.mutate({
mutation: CREATE_ACTIE_QUERY,
variables: {
// ...
}
})
.then(response => {
console.log(response.data.createActie);
})
but now i want to extract the id and i struggle to say the least (i am not a trained javascript developer, just trying to learn by reading and experimenting)
the solution was adding json.parse...
JSON.parse(response.data.createActie)[0][0].id
Here is the code for when I'm pushing the data to Firebase:
firebase.database().ref(`booklogs/${uid}/${book_id}`).push(page_id)
booklogs :
{HUMjSHxVKAPfVXzOId9zCBkGOgv1:{
book28917: {
-KYp4FdYYODDZG1FX-Pb: 1
}
}
}
My problem is when I query the data, the child node of the ${book_id} includes the push key, but I only want to get the value which is 1 and not the push key.
The code I use to query is:
var booklogs = db.ref(`booklogs/${uid}/${project}`);
booklogs.once('value')
.then((snapshot) => {
console.log(`pages viewed are ${snapshot.key}: ${snapshot.val()}`);
console.dir(snapshot.val());
}).catch((error) => {
console.log(`Error : ${error}`);
});
The data returned in the console is:
pages viewed are 2634651: [object Object]
{ '-KYp4FdYYODDZG1FX-Pb': 1 }
Any input would be much appreciated. Thanks!
If you only want the '1' and not the push key, try using .set()
firebase.database().ref(`booklogs/${uid}/${book_id}`).set(page_id)
That will get rid of the object and just give you the value that you wanted. Push automatically generates a key for every value you add, so you will always get an object back. From the Firebase docs - "For basic write operations, you can use set() to save data to a specified reference, replacing any existing data at that path."
https://firebase.google.com/docs/database/web/read-and-write
I am trying to interrogate an API response from the Recognize (fashion recognition) API. The data is returned as set out below. I am trying to extract the items of attire from the following object.
Object {data: " Array↵(↵ [id] => 1309↵)↵{"Status":true,"Data":{"VufindTags":["Dress"," Purse"]}}", status: 200, headers: function, config: Object, statusText: "OK"}config: Objectdata: " Array↵(↵ [id] => 1309↵)↵{"Status":true,"Data":{"VufindTags":["Dress"," Purse"]}}"headers: function (name) {status: 200statusText: "OK"__proto__: Object
I have tried to access using data.data which returned the following as a string:
" Array
(
[id] => 1309
)
{"Status":true,"Data":{"VufindTags":["Dress"," Purse"]}}"
I then tried to use JSON.parse to extract the data from the VufindTags. That did not work.
Is there a way to convert this into a JSON Object??
Thanks for any help!!
It looks like the vufind API is giving you PHP print_r output instead of JSON. The best thing to do would be to get them to fix their API. Failing that, you can pull the JSON-ified bits out. I had some success with this:
myObj = JSON.parse(apiOutput.slice(apiOutput.indexOf('{')))
...but I wouldn't put that into an app and call it production ready, especially when the API clearly isn't giving you what it should in the first place.