English 中文(简体)
Vue3 Fetch getting data but not populating dropdown fields. :( What am I doing wrong?
原标题:

I have created a company to try to download incident data from the North Carolina determine the transportation so that I can put it on a map. However, while it is downloading the data (418 records), it is not splitting it up into the appropriate sections so that it can be rendered in the select/option drop downs. I am not sure why this is not happening. I ve been noodling on this for at least an hour and I just have not been getting any luck regardless of asking ChaT GPT, and doing a lot of look on Stack Overflow, etc.

INPUT: https://eapps.ncdot.gov/services/traffic-prod/v1/incidents EXPECTED OUTPUT: Dropdowns populated with data ACTUAL: Dropdowns but no data, however call resuls in 418 records, so?

Any of my fellow SO s have any insights? Much appreciated.

    <template>
  <div>
    <div>to wit: {{ dropdownData }}</div>
    <select v-model="reason">
      <option v-for="r in dropdownData.reasons" :value="r">{{ r }}</option>
    </select>
    <select v-model="condition">
      <option v-for="c in dropdownData.conditions" :value="c">{{ c }}</option>
    </select>
    <select v-model="incidentType">
      <option v-for="type in dropdownData.incidentTypes" :value="type">{{ type }}</option>
    </select>
    <button @click="getData">Get Data</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      reason: null,
      condition: null,
      incidentType: null,
      dropdownData: {
        reasons: [],
        conditions: [],
        incidentTypes: []
      }
    }
  },
  mounted() {
    this.fetchDropdownData()
  },
  methods: {
    fetchDropdownData() {

// actual API: https://eapps.ncdot.gov/services/traffic-prod/v1/incidents?verbose=true

      fetch(`${import.meta.env.VITE_API_VERBOSE}`)
        .then((response) => {
          if (!response.ok) {
            throw new Error( Network response was not ok )
          }
          return response.json()
        })
        .then((data) => {
          // Create a new reactive object using Vue.set or spread operator
          this.dropdownData = {
            reasons: [...data.reasons],
            conditions: [...data.conditions],
            incidentTypes: [...data.incidentTypes]
          }
        })
        .catch((error) => {
          // Handle error
          console.error( Error: , error)
        })
    },
    getData() {
      // Make use of the selected values: this.reason, this.condition, this.incidentType
      // Perform further operations or API calls based on the selected values
    }
  }
}
</script>

Codepen: https://codepen.io/MaxRocket/pen/wvYVKJY

Thoughts, my fellow geeks?

Here are a few suggestions from Cody, but I don t think they apply, except maybe #7, but I thought I d share them anyway.

There are a few possible reasons the select options may not be populating:

  1. The dropdownData object is empty. If the API call to populate the data has not completed or failed, dropdownData will be empty and no options will render.

  2. The v-for directive is iterating over an empty array. If dropdownData.reasons, dropdownData.conditions, or dropdownData.incidentTypes are empty arrays, no options will be rendered.

  3. There is a syntax error in the template preventing it from compiling. Double check that all v-for directives, v-models, etc. have correct syntax.

  4. The data function is not returning the dropdownData object. Make sure data() returns dropdownData, and that it is not undefined.

  5. There is an error in the mounted lifecycle hook preventing the API call from completing. Check the network tab to see if the API call is failing, and double check for any errors in the mounted function.

  6. The select element has a v-model but no value set for the options. For a v-model to work, each must have a value attribute set.

  7. The component is not actually using the dropdownData data. Make sure v-for is iterating over dropdownData, and v-model is using the data from dropdownData.

问题回答

The problem is this code

reasons: [...data.reasons],
conditions: [...data.conditions],
incidentTypes: [...data.incidentTypes]

Your data returns an array of item, in each item has properties like reason, condition and incidentType. You should create an array base on that items. For example

 reasons: data.map(el => el.reason),
 conditions: data.map(el => el.condition),
 ncidentTypes: data.map(el =>el.incidentType)




相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

getting XML from other domain using ASP.NET

I m fairly new to ASP.NET. And I was wondering how I could go about getting xml from a site (Kuler s API in this case), and then post the result using AJAX? So what I want here, is to be able to do a ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

UML Diagram to Model API

I need to create a diagram to document a RESTFul API that build, which UML diagram should I use? Thanks in advance,

How best to expose Rails methods via an API?

Let s say I have a model foo, and my model has a publish! method that changes a few properties on that model and potentially a few others too. Now, the Rails way suggests that I expose my model over ...

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

About paypal express checkout api

In this picture,there are 3 main steps:SetExpressCheckout,GetExpressCheckoutDetails and DoExpressCheckoutDetails,I m now sure SetExpressCheckout is to be called by myself,what about ...

热门标签