What is the best way to store API data obtained from a v-for loop into my MongoDB database?

Within my Vue application, I am working with API data and attempting to extract the value from {{ currency.description }} for database posting.

I have experimented with different methods like using

PostService.insertPost(e.target.innerHTML);

Another attempt was made by accessing this.$refs.criteria[0].innerHTML

While the console logs the desired output correctly, it seems to turn null during the database post.

Including VUE template as follows:

<ul  v-bind:key="currency.id" ref="criteria" v-for="currency in info">
  <li id="criteria" v-on:click="apiTest" >{{ currency.description }}</li>
</ul>

Corresponding JS code:

apiTest(e) {   
  console.log(e.target.innerHTML);
  await PostService.insertPost(e.target.innerHTML);
}

data() {
  return{
    info: [],

The main objective is to successfully store the currency.description string in the database rather than encountering a null result. It is likely related to how the information is handled within the info array of my data return statement, but finding the correct approach remains uncertain to me.

Answer №1

To create a loop on the li element instead, modify your code to pass the description to the click function being called. Here is an example:

// Updated Template
<template>
  <ul>
    <li
      v-for="currency in info"
      v-bind:key="currency.id"
      v-on:click="apiCall(currency.description)">
      {{ currency.description }}
    </li>
  </ul>
</template>

// Revised Method / Click function
async apiCall(description) {
  await PostService.createPost(description);
}

Answer №2

Consider utilizing await PostService.insertPost(currency.description); within the $nextTick function. It's possible that the issue stems from a delay in DOM loading while iterating through the list items.

Answer №3

<ul>
    <li v-for="currency in info" :id="currency.id" @click="apiTest(currency)">{{ currency.description }} </li>
</ul>

apiTest: function (currency){
    //You have access to the HTML element
    //var element = document.getElementById(currency.id)
    console.log(currency.description);
    await PostService.insertPost(currency.description);

}

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

Ordering in D3 Code

I'm struggling with understanding the correct structure for writing D3 code. For example: In the code snippet below, I noticed that if I place the 3rd section (Chart Title) of the code after creating the svg element, the title text doesn't displ ...

A function in Angular 2 that subscribes to an Http Observable callback

I am facing an issue with the .subscribe function in my code. Firstly, I have a function that sends a request to the server. protected sendRequest(data:string,url:string):Observable<any>{ let headers = new Headers({ 'Content-Type': &a ...

Is there a way to dynamically include a peer dependency in a file that is bundled using webpack?

I am currently developing a plugin that relies on jQuery as a peer dependency. However, when I attempt to import this plugin into my primary project (which already has jQuery installed), I encounter the following error: Module not found: Error: Can't ...

Vuetify vintage vibes

I recently started working with Vue/Vuetify and decided to use the vuetify v-text-field tag with the outline prop. Why is the label displaying like it is in the image? Additionally, the font doesn't appear to be the Vuetify default one. This is my ...

Issue: ng-file-upload validation is not functioning correctly, resulting in the form always being considered valid

I'm looking to implement a file-upload feature for each item in an array. In order to achieve this, I am utilizing the ng-repeat directive to cycle through the array and incorporating the ng-file-upload plugin to manage the file upload process along w ...

`Testing the functionality of javascript/jQuery events using Jasmine`

I came across this code snippet: $(document).on('click', '#clear-button', clearCalculatedPrice) clearCalculatedPrice = -> $('#price_rule').removeAttr('data-original-title') $('#calculated-price&apos ...

What is the reason behind the default disabling of bootstrap multiselect options?

Just diving into the world of bootstrap and I'm puzzled as to why my simple multiselect options aren't responding when clicked. UPDATE The first multiselect option on the test-site linked below is the one giving me trouble. I've tried it o ...

JQuery Form Wizard - Adding a Finish Button Linked to a Page

Having some difficulty linking the finish button on my Jquery Form Wizard within my Flask App to a link. I attempted to test the functionality by creating a simple alert, but that didn't work either. I'm certain that I missed a step somewhere, b ...

When implementing $().html, certain impacts of mdui may fade away

I've been exploring the functionalities provided by mdui.org, specifically the panel feature. Initially, I had successfully integrated the codes into the HTML body with no issues. View the code here See the screen capture here This allowed me to to ...

Tips on using constructor functions and the new keyword in Typescript

This is a demonstration taken from the MDN documentation showcasing the usage of the new keyword function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } const car1 = new Car('Eagle', 'Talon TSi&apos ...

Refining information and displaying it alongside the "indelible" data - react

I recently implemented a TextField component from the MUI library, along with a useRef hook to capture user input "live" as they type. The goal is to filter rates based on the characters entered by the user. Currently, I am working with an array of rate ke ...

The pinFileToIPFS method in Pinata IPFS is currently unable to accept files uploaded by users

Currently, I am immersed in a project where I am utilizing React.js, Express.js, and Node.js to transform a user-provided image into an NFT on the Ethereum blockchain. To achieve this, I must first upload the image to IPFS (Pinata being my choice of servic ...

Simultaneously updating the states in both the child and parent components when clicked

In my code, I have two components: the parent component where a prop is passed in for changing state and the child component where the function is called. The function changes the state of the parent component based on an index. changeState={() => this ...

Creating a dynamic button with Vue

I need help with rendering a button on a Vue instance. I want to be able to click on the button and have another button render with a click event. When I simply mount the button, the function doesn't work. const Hello = { props: ['text'], ...

Tips for adjusting the class of elements in neighboring rows of an HTML table with the help of jQuery's nextAll() selector

While attempting to switch classes using the jQuery selector .nextAll(':lt(3)'), I encountered an issue. The class change does not take effect on new rows of HTML tables. It seems like the class change for the next 3 cells is only being applied ...

Using Mongodb aggregation to group items within the past 5 days

I'm struggling to obtain the desired outcome using mongodb aggregation. Here is an example document from the collection: [{ "_id": "34243243243", "workType": "TESTWORK1", "assignedDate":ISODate(&qu ...

What steps can be taken to fix a syntax error in a NodeJS express server code?

I am currently facing a syntax error in the code below, and I'm struggling to fix it. The specific error message is as follows: node staticapi.js /Users/v/Desktop/CS-Extra/EIP/A5/staticapi.js:123 res.status(200).send("Api is running") ...

Creating a Custom Hot Module Replacement Hook for Vue.js and Webpack

Is there a way to create a hook that triggers when a Vue component is updated using hot module replacement? [HMR] App is up to date. Now I want to execute a specific method after the update. ...

Creating SVG Lines with Google Maps JavaScript API v3

I have a project that requires a dashed line between two points on Google Maps using JavaScript v3. The specification states that each dash should be 100px long. I have attempted to achieve this using SVG, but the dashes are not displaying correctly. Here ...

What could be the reason for the absence of the HTTP response in the network tab of the Chrome debugger?

After utilizing Postman to test my web service, I was able to find the WS's response in the http response body. However, when I made a call using ajax in my web application, I encountered an issue where I could no longer locate the response. The tab ...