简体   繁体   English

如何将一些API数据从v-for循环保存到我的mongodb?

[英]How do I save some API data from a v-for loop to my mongodb?

I am looping through some API data in Vue. 我在Vue中循环一些API数据。 I want to get the value that is displayed in my braces {{ currency.description }} and post that to my db. 我想得到我的大括号{{ currency.description }}显示的值并将其发布到我的数据库。

I have tried for example PostService.insertPost(e.target.innerHTML); 我试过PostService.insertPost(e.target.innerHTML);

Also have tried this.$refs.criteria[0].innerHTML 也试过this.$refs.criteria[0].innerHTML

When console logged it shows exactly what I need, but when posted to the db it shows null. 当控制台记录时,它会准确显示我需要的内容,但是当发布到数据库时,它显示为null。

VUE template VUE模板

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

JS JS

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

data() {
  return{
    info: [],

My goal is to get the string value of currency.description into my db, instead of it showing up as null . 我的目标是将currency.description的字符串值输入到我的数据库中,而不是显示为null I am guessing it has to do with my info array in my returned data, I am not sure how to go about it though. 我猜测它与我的返回数据中的信息数组有关,我不知道如何去做。

You want to make the loop on the li -element instead, and pass the description to the click function you're calling, like so: 您想要在li -element上创建循环,并将描述传递给您正在调用的click函数,如下所示:

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

// Method / Click function
async apiTest(description) {
  await PostService.insertPost(description);
}

Try using await PostService.insertPost(currency.description); 尝试使用await PostService.insertPost(currency.description); in $nextTick function. 在$ nextTick函数中。 I think the problem may be the delay in loading the DOM since it is looping the list items. 我认为问题可能是加载DOM的延迟,因为它循环列表项。

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

apiTest: function (currency){
    //You can get the html element
    //var element = document,getElementById(currency.id)
    console.log(currency.description);
    await PostService.insertPost(currency.description);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM