How to store the user selected value from API response in a variable

user16779293

User inputs a company's ID number and it will return them a list of addresses that belong to that company split in 3 objects (street, zip code, city).

When they click on one of those addresses, I want the 3 elements of the selected address to be stored in the postData() function:

<template>
    <div>
      <v-card-text>
        <v-row v-if="ask_id">
          <v-text-field label="Enter the id_number" v-model="id_number"></v-text-field>
          <v-btn 
          @click="
          send_id_number();
          show_results()">
          </v-btn>
        </v-row>
        <v-row v-else>
          <v-select v-model="res_data"
          :items="filteredData" label="Places" :item-text="
          (item) => item.street + item.zip_code + item.city">
          </v-select>
        </v-row>
      </v-card-text>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    props: ["id"],
    data() {
      return {
        ask_id: true,
        id_number: null,
        res_object: [],
        res_data: [],
        places: []
      };
    },
    computed: {
        filteredData() {
        return this.res_object.places
          ? this.res_object.places.filter((g) => {
            return g.places_closed == false;
          })
          : [];
      },
      postData() {
        let data = {
            selected_street: this.?,
            selected_zip: this.?,
            selected_city: this.?,
            id_number: this.?
        }
        return data
      }
    },
    methods: {
      show_results() {
      this.mode = false
      },
      send_id_number() {
        axios.get(`https://api.places_url.com/${this.id_number}`)
        .then((response) => {
          this.res_object = response.data
        })
        .catch((err) => {
          console.log(err)
        })
      }
    },
  };
  </script>

However whatever I try to do tells me TypeError: undefined is not an object. I'm missing a step but I'm not sure what it is.

structure of the api response:

{
    "company_id": 123456789,
    "places":[
        {"street":"8 main street",
        "zip_code":"303",
        "city":"RandomCity",
        "places_closed":false
        },
        {"street":"10 main street",
        "zip_code":"404",
        "city":"RandomCity",
        "places_closed":false
        },
        {"street":"11 main street",
        "zip_code":"505",
        "city":"RandomCity",
        "places_closed":false
        }
    ]
}
Nilesh Mishra

you can try something like the below add return-object props in v-select and then you get res_data v-model as an object.

    <template>
    <div>
      <v-card-text>
        <v-row v-if="ask_id">
          <v-text-field label="Enter the id_number" v-model="id_number"></v-text-field>
          <v-btn 
          @click="
          send_id_number();
          show_results()">
          </v-btn>
        </v-row>
        <v-row v-else>
          <v-select @change="postData()" v-model="res_data"
          :items="filteredData" label="Places" :item-text="
          (item) => item.street +' '+ item.zip_code +' '+ item.city" return-object>
          </v-select>
        </v-row>
      </v-card-text>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    props: ["id"],
    data() {
      return {
        ask_id: false,
        id_number: null,
        res_object: 
        {
    "company_id": 123456789,
    "places":[
        {"street":"8 main street",
        "zip_code":"303",
        "city":"RandomCity",
        "places_closed":false
        },
        {"street":"10 main street",
        "zip_code":"404",
        "city":"RandomCity",
        "places_closed":false
        },
        {"street":"11 main street",
        "zip_code":"505",
        "city":"RandomCity",
        "places_closed":false
        }
    ]
},
        res_data: [],
        places: []
      };
    },
    computed: {
        filteredData() {
        return this.res_object.places
          ? this.res_object.places.filter((g) => {
            return g.places_closed == false;
          })
          : [];
      },

      
    },
    
    methods: {
      postData() {
        console.log(this.res_data);
        return this.res_data;
      },
      show_results() {
      this.mode = false
      },
      send_id_number() {
        console.log(this.id_number);
        axios.get(`https://api.places_url.com/${this.id_number}`)
        .then((response) => {
          this.res_object = response.data
        })
        .catch((err) => {
          console.log(err)
        })
      }
    },
  };
  </script>

Let me know if you want something else

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store the selected value in a variable?

How to store in a variable, the range address from a user selected range

How to extract and store a value from jmeter response and store inn csv?

How to get the value of a selected option dynamically and store it into a variable?

Python - How to parse xml response and store a elements value in a variable?

How to store value from grep output in variable

How to store a value from firebase in a global variable?

How to store value from datepicker into string variable

How to store response in a variable in Volley?

How can I store the value a user enters in a variable?

How to store data from json API response into array in ReactJS?

Flutter: How to store specifc data from api response in a list

How to store a complex json response from api in local database in flutter?

How to store a JSON response from the API IGDB into an array

Store the response value in a variable in ROBOT Framework

How to read a value from a text file and store that value into a temporary variable?

bash - loop and nested variable to store api response

How to get user selected value from SelectField in python Flask WTF?

How to get string value from Listbox as user selected?

How to store user input from DOM in Javascript variable?

Catel: MVVM how to get selected value from a listview and save it to a variable?

How to get value of variable from selected class In c#?

How to set javascript variable value from selected textboxes to another textbox

How to assign a value selected from a Picker to a variable in another class?

How to set the value of a PHP variable to a selected value from a select box as it is selected?

How to store the selected radio-button value of a user after filling the registration form into database in django?

Store value from JSON to a variable

How to store in a JavaScript variable the selected rows in a table

How to store user selected zoom for future sessions without using cookies Google Geocoder API