Using Vue with Axios allows for making a GET request with a variable that is obtained from a separate GET request

After successfully building a component that showcases 3D print information, known as a fabmoment, I managed to populate an author-object and a fabmoment-object with data using the $route.params. Here's how:

<script>
import SingleSummarized from './SingleSummarized'
// import Comments from '@/components/Comments/Multiple'
export default {
  name: 'Single',
  data () {
    return {
      author: null,
      fabmoment: null,
      tempLicenseID: null,
      license: null
    }
  },
  created () {
    this.$http.get(`/users/${this.$route.params.author_id}`)
        .then(request => { this.author = request.data })
        .catch(() => { alert('Something went wrong when trying to retrieve this user!') })
    this.$http.get(`/users/${this.$route.params.author_id}/fabmoments/${this.$route.params.fabmoment_id}`)
        .then(request => { this.fabmoment = request.data })
        .catch(() => { alert('Something went wrong when trying to retrieve the fabmoment attribute data!') })
    this.$http.get(`/licenses/${this.fabmoment.license_id`)
        .then(request => { this.license = request.data })
        .catch(() => { alert('Something went wrong when trying to retrieve the license!') })
  },
  components: {
    SingleSummarized
    // Comments
  }
}
</script>

A problem arises in the code where I attempt to fetch a license for the fabmoment using this.fabmoment.license_id. It doesn't work at this point.

Would implementing a tempLicenseID be a viable solution? Since it appears that this.fabmoment is not accessible yet. How can I adjust this to ensure the request is successful? Any better approaches to consider?

Answer №1

Once you have the fabmoment object, remember to request the licenses:

this.$http.get(`/users/${this.$route.params.author_id}/fabmoments/${this.$route.params.fabmoment_id}`)
    .then(request => { return this.fabmoment = request.data })
    .then( fabmoment => this.$http.get(`/licenses/${fabmoment.license_id`)
    .then(request => { this.license = request.data })
    .catch(() => { alert('There was an issue retrieving the fabmoment attribute data!') })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JS Plugin Loading Problem

What combination of libraries/frameworks would work best for optimizing performance in an HTML5/CSS3/JS app with moving elements? I have done some research, but I am venturing into unfamiliar territory when it comes to performance. Are there key principles ...

Is there a way to search through an array of object arrays in JavaScript for a specific key/value pair using lodash or any other function?

I am faced with a task involving an array of objects. Each object has a key that needs to be used to search through sets of arrays containing similar objects. The goal is to determine if all the arrays contain the same value for a specific key in my object ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

Error message in Angular 7: Issue with @ViewChild - Unable to access properties from undefined value (specifically 'next')

I have two components that I want to connect with each other. One component is a modal dialog containing a button that, when clicked, should advance the application to the next step. The second component is an Angular Material stepper. How can I achieve th ...

Optimal row settings for different reports using Material-UI table pagination

Recently, I've been exploring an example involving material-ui's TablePagination. In this scenario, they present a useTable component with the following code snippet: import React, { useState } from 'react' import { Table, TableHead, Ta ...

Obtaining the website link using Django's template language

Is there a way in the Django template language to reference the URL? I want to use it on static pages to avoid live links. Can this be accomplished without JavaScript? I am hoping to achieve something similar to: {% if current_url == "/about/" %} About { ...

Exploring the functionality of Angular's Controller As using Jasmine testing framework

I am a beginner in Jasmine/Angular testing and I'm attempting to test a controller that I have created. The code for the controller is shown below: (function () { 'use strict'; angular .module('App') .controller('Act ...

Identifying when the mouse hovers over a hidden element

Is there a way to make events trigger for <mark> elements under a <textarea>? I've tried adding mouseover listeners, but they don't seem to work because of the <textarea>. I need the <text-area> to function normally, so u ...

Generate a custom JavaScript file designed for compatibility with multiple servers

I am looking to develop a JavaScript file that can be easily added to other websites, allowing them to access functions within my database using an API similar to the Facebook like button. This API should display the total likes and show which friends have ...

What is the best way to encode a JSON object using Jquery Week Calendar under the subject "JSON parsing"?

I am currently utilizing Week Calendar (https://github.com/themouette/jquery-week-calendar/wiki) in my ruby on rails project. However, I am encountering an issue with the JSON object in my ruby code that is supposed to return JSON data to the weekcalendar. ...

Setting initial values for multiple elements in JavaScript

Attempting to set default values for DOM elements with JavaScript involves checking if the field value is empty. If it is empty, the value should be changed to 0; otherwise, no action is needed. Text fields: <input class="field" type="text" name="feat ...

Reactjs - Creating a video component with a placeholder that loads the video seamlessly

I created a Video component that utilizes the React.Suspense component to show a placeholder while the video is loading. However, I'm facing an issue where it seems like the functionality isn't working as intended. When I set the network to "slow ...

When attempting to define a route as a lambda function in vue-cli, the default lazy load code does not function

After using vue-cli to set up a new Typescript project with vue-router included, I noticed that the generated router/index.ts configuration looked like this: const routes: Array<RouteConfig> = [ { path: '/', name: 'Home' ...

Leveraging Selenium Web Driver to interact with JavaScript global variables within Jquery

I'm attempting to perform a functional test in Python using Selenium and need to retrieve the value of a global variable in JavaScript that was declared on a specific page. Typically, browser.execute_script("return globalVar;") works as expected. How ...

The issue arises when trying to use destructured imports with Mongoose

I've been developing a straightforward Express app with ES6. In the process of creating a schema and model for Mongoose, I encountered an issue with the following syntax: import mongoose, { Schema } from 'mongoose'; const PostSchema = new ...

[Vue alert]: Element with id #app not found

I have set up a new Laravel project with all the components updated and running smoothly. Recently, I attempted to include app.js in my welcome.blade.php file. However, upon adding it, I encountered the following error: [Vue warn]: Cannot find element: ...

Submitting the form does not result in the textbox being cleared

I have been attempting to clear the txtSubTotal text box upon clicking the PROCEED button, but it seems that my efforts have been in vain despite trying various code examples, including those from SO. btnProceed/HTML <input type="submit" name="btnProc ...

Shuffle array elements in JavaScript

I need help with manipulating an array containing nested arrays. Here's an example: const arr = [[red,green],[house,roof,wall]] Is there a method to combine the nested arrays so that the output is formatted like this? red house, red roof, red wall, g ...

The "users" property or method has not been defined in Vue.js applications

During the development of my Vue.js shopping cart, I encountered an issue where I wanted to display the username in a label after the user provided correct information. However, I received the following error in Google Chrome: vue.js:634 [Vue warn]: Prope ...

There is no matching overload for this call in React Native

I am working on organizing the styles for elements in order to enhance readability. Here is the code I have written: let styles={ search:{ container:{ position:"absolute", top:0, }, } } After defining the s ...