Split data JSON after data in VueJS - laravel

How to split data in JSON if have array after array? This is my JSON format
{
"status": "true",
"error": "",
"data": [
{
"id": 1,
"sections": [
{
"section_id": 1,
"position": []
}
]
}
]
}
So, I'm using AXIOS . This code AXIOS
const url = '/api/v1/form/'
axios
.get(url)
.then(response => {
this.listing = response.data.data;
})
How to call section using this response.data.data.sections ? I'm try but error undefined. Please help me thanks

response.data.data[0].sections
data is an array according to your json so you cannot directly call sections you will have to iterate or select an instance within the array.
the following code should print all section ids //untested code
const url = '/api/v1/form/'
axios
.get(url)
.then(response => {
response.data.data.foreach((listing)=>{
console.log(listing.sections.section_id)
})
})
If you always will have only one entry under data or you want to access only the 1st entry of data you can use response.data.data[0].sections this is a bad way to access it though because if data is empty it will throw you an error . If the case is that you only have one entry under data you should just change your json to be
{
"status": "true",
"error": "",
"data":
{
"id": 1,
"sections": [
{
"section_id": 1,
"position": []
}
]
}
}
and you can then access it directly response.data.data.sections but as long as its an array you will have to treat it as such.
Iterate through sections and positions [as per comments]:
const url = '/api/v1/form/'
axios
.get(url)
.then(response => {
response.data.data.foreach((listing)=>{ //#this will get you each listing(parent object that contains sections array)
listing.sections.foreach((section)=>{//# this will get you each section(parent object that contains position array)
section.position.foreach((el)=>{//# this will get you each elment in the position array as `el`
console.log(el)
})
})
})
})

Try this:
const url = '/api/v1/form/'
axios
.get(url)
.then(response => {
this.listing = response.data;
console.log(this.listing.data) // <-------
})

Related

How to get cypress to dynamically update a fixture to a graphql call

I need to mutate a graphql call in Cypress to where I can change certain key:value pairs within a fixture depending on the test.
I know I can call a graphql fixture with a format similar to this:
cy.intercept('POST', '/graphql', (req) => {
if (req.body.operationName === 'operationName') {
req.reply({ fixture: 'mockData.json'});
}
}
but pretend mockData has the following shape:
data: {
"user": {
"user": {
"userId": 123,
"inviteId": "123",
"status": "NEEDS_MORE",
"firstName": "Testy",
"lastName": "McTesterson",
"email": "test#testqa.com",
"phone": "3129876767",
"user": null,
"invite": {
"id": "12345",
"sentAt": null,
"sendDate": null,
"status": "NOT_SENT",
"__typename": "Invite"
},
},
}
}
How would I intercept the graphql call with all the info in mockData.json but change "status": "NEEDS_MORE" to "status": "CLAIMED" in my test without changing the rest of the fixture? The idea would be that in each it block of a spec, I re-use the same fixture but change the status, and have different assertions.
My attempts so far either only send the status without the rest of the data or only send the fixture without mutating anything. There is cypress documentation on how to do this in rest, but not in graphql. We're using typescript.
You can read the fixture first, make the modification before the reply
cy.fixture('mockData.json').then(data => {
data.user.user.status = 'CLAIMED'
cy.intercept('POST', '/graphql', (req) => {
// according to one reference, this is needed
const g = JSON.parse(req.body)
if (g.operationName === 'operationName') {
req.reply({
body: {data} // same reference shows reply stubbing in this format
})
}
}
})
Ref: Smart GraphQL Stubbing in Cypress
Consider using a dynamic import to read the fixture.
cy.intercept('POST', '**/graphql', async (req) => {
const data = await import('../fixtures/mockData.json')
data.user.user.status = 'CLAIMED'
if (req.body.operationName === 'operationName') {
req.reply({
body: {data}
})
}
})
Since you have typescript set up, add resolveJsonModule to tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"],
"resolveJsonModule": true
},
"include": ["**/*.ts"]
}
#Paolo's answer is good. An alternative to avoid using cy.fixture().then()
const mockedData = require('path/to/fixture/mockData.json')
// later in your test block
mockedData.user.user.status = 'CLAIMED'
cy.intercept('POST', '/graphql', (req) => {
if (req.body.operationName === 'operationName') {
req.reply((res) => (res.body.data = mockedData))
}
})

how to get response from axios with multiable objects?

I am getting response payload from server like this:
{"0":
[
{"id":1,
"profile_id":"1",
"schoolName":"xx",
"courseName":"xx",
"courseDescription":null,
"created_at":"2020-06-25T09:11:21.000000Z",
"updated_at":"2020-06-25T09:11:21.000000Z"},
{"id":2,
"profile_id":"1",
"schoolName":"yy",
"courseName":"zz",
"courseDescription":"xx",
"created_at":null,
"updated_at":null}
],
"status":"Read Education info successfully!"}
Axios .then response is like this:
.then((response)=>{
vm.setState (
{ educations: Object.values(response.data) },
() => console.log(this.state.datas)
);
})
data- object has educations: []- array inside multiable
education: -objects
Q) how to pass axios response data payload to education object?,
export default {
data() {
return {
educations: [
education: {}],
--- Edit ---
Tried to get response like this:
.then((response)=>{
this.education = response.data.map(value => {
return {
schoolName: value.schoolName,
courseName: value.courseName
}
console.log('Receiving data ....')
})
Vue does not give any error, but I cannot see any data to pass data-object.
In Vuestools:
education : object
courseName: "" is empty.

VueJS - Can't access assigned this.property declared within mounted() in mounted()

I'm doing a project where I'm fetching an api data from my backend laravel with axios get and using its property to concatenate a channel within the mounted section of my component.
I'm fetching api/user through axios get which contains
{
"id": 1,
"name": "admin",
"email": "admin#company.com",
"email_verified_at": null,
"created_at": "2019-08-17 10:06:14",
"updated_at": "2019-11-27 02:03:30",
"phone_number": "+12345678911",
"phone_number_verified": 0,
"company_id": 1,
"verification_code": "",
"signature": "",
"signature_image": null,
}
Here's how I'm getting the user data from my laravel backend through axios:
mounted() {
this.$http
.get('/api/user')
.then(response => ( this.user = response.data ))
.catch(error => console.log(error))
},
Which is then declared as an empty array in my data() section as such:
data() {
return {
user: [],
};
},
I can access the properties for example, id, in my template through {{ user.id }} and when I try to access it within the mounted() section, it gives me an object but when I try console.log(this.user.id) within the mounted, it throws undefined instead. The reason I need to access this.user.id within the mounted section is because I'm using it to concatenate the channel I'm using through Laravel Echo given as such:
mounted() {
this.$http
.get('/api/user')
.then(response => ( this.user = response.data ))
.catch(error => console.log(error))
this.$echo
.channel(`new-notification.${this.user.id}`)
.listen('NewNotification', (data) => {
// eslint-disable-next-line no-alert
this.notifications.unshift(data);
});
},
I'm fairly new to Vue and I don't know what I'm doing wrong in this. It would be of great help if someone can point out what I'm doing wrong or if there's a better method of doing it.
The issue is the console will run before the response of api call. You can make the call async and try.
mounted() {
this.fetchApi();
},
methods:{
fetchApi: async function(){
let response = await this.$http
.get('/api/user');
this.user = response.data;
this.$echo
.channel(`new-notification.${response.data.id}`)
.listen('NewNotification', (data) => {
// eslint-disable-next-line no-alert
this.notifications.unshift(data);
});
}
}

Vue.js 2 & Axios - Filtering an api for a search feature

I'm trying to filter through a collection of films that i'm retrieving using axios. This is so i can compare it to a search string for a search feature. Everything works fine except when using the computed property it returns Cannot read property 'filter' of undefined" but when i check the vue dev tool it says that the computed property contains the array of films which doesn't really add up. The code is as follows.
created(){
this.fetchFilms();
},
methods:{
fetchFilms(page_url){
let vm = this;
// storing the page url
page_url = page_url || '/api/films'
axios.get(page_url)
.then(response => response)
.then(response => {
this.films = response.data;
vm.makePagination(response.meta, response.links);
})
.catch(err => console.log(err));
},
makePagination(meta,links){
let pagination = {
current_page: this.films.meta.current_page,
last_page: this.films.meta.last_page,
next_page_url: this.films.links.next,
prev_page_url: this.films.links.prev
}
this.pagination = pagination;
}
},
computed: {
filteredFilms () {
return this.films.data.filter((film) => {
return film.film_name.toLowerCase().match(this.searchString.toLowerCase())
})
},
}
This is how the data is returned
films:Object
data:Array[10]
links:Object
meta:Object
Any help is appreciated.
You're probably accessing filteredFilms before the request is done. I don't see any code to wait for the request. You could make filteredFilms check if the data is there and return an empty list if it isn't.

Axios + Vue + Vuex manipulating a basic GET with deeply nested data

I am trying to make a basic GET call to an API using JSON API specifications with deeply nested relationship data. I'm experiencing an issue with axios or vue manipulating my data...
Paying attention to pickup and delivery relationships, the raw data looks like the following:
{
"data": {
"type": "parent",
"id": "34",
"attributes": {
// ...
},
"relationships": {
"r1": {
"data": [
{
"type": "nextparent",
"id": "62",
"attributes": {
// ..
},
"relationships": {
"pickup": {
"data": [
{
"type": "package",
"id": 521,
"attributes": {
// ...
}
},
// ...
]
},
"delivery": {
"data": []
}
}
},
// ...
]
}
}
}
}
... where pickup has an array of objects and delivery has an empty array. However, when I try to retrieve the response in the callback, delivery is an exact copy of pickup.
var promise = window.axios({
url: window.baseApi + '/my-object/34',
method: 'get'
})
promise.then(function (response) {
console.log(response)
})
Whenever you run console.log(response) in the callback, Vue's observers are applied to the response object, which makes me wonder if this is a Vue issue considering that only the relationships of the first r1 object experience this.
Screenshot of log from callback (incorrect):
Additionally, I checked the response from the axios transformResponse function and the raw json was correct (without observers). This is only happening in the callback.
Screenshot of log from transformResponse (correct):
Will update with fiddle, as soon as I can.
EDIT:
The axios function is being called in a vuex action. Here is the code:
fetchData ({commit, dispatch}) {
var promise = window.axios({
url: window.baseApi + '/my-object/34',
method: 'get'
})
promise
.then(function (response) {
console.log(response)
commit('toggleProgress', {key: 'fetchingData'})
commit('setActive', response.data.data)
dispatch('otherModule/setActiveShipments', response.data.data, {root: true})
dispatch('history/clear')
})
.catch(function () {
//
})
return promise
},

Resources