[Vue warn]: Duplicate keys detected: x. This may cause an update error. How to prevent adding item which has already been added via method

I have a v-autocomplete that loops a list of users. I then add them to a new list via a btn. How to I stop and alert that a user has already been added by comparing the dup key?

<v-autocomplete
    v-model="event.user"
    :items="usersData"
    label="Seach speaker list"
    :search-input.sync="searchUser"
    return-object
    item-value="id"
    item-text="name"
></v-autocomplete>

My method to addSpeaker is:

addSpeaker() {
    const newSpeaker = {
        id: this.event.user.uid,
        name: this.event.user.name,
    }
    this.speakers.push(newSpeaker)
    this.event.user = ''
},
removeSpeaker(id) {
    this.speakers = this.speakers.filter(speaker => speaker.id !== id)
}

Is there a check I can do here to prevent a user being added if it already has been?

29 thoughts on “[Vue warn]: Duplicate keys detected: x. This may cause an update error. How to prevent adding item which has already been added via method”

  1. Check if the user exists by using id. If the user exists then show an error, else add the user:

    addSpeaker () {
      const newSpeaker = {
        id: this.event.user.uid,
        name: this.event.user.name,
      }
    
      let doesUserExists = false;
    
      for (let i = 0; i < this.speakers.length; i++) {
        if (this.speakers[i].id === newSpeaker.id) {
          // Show an error that the user does already exists
    
          doesUserExists = true;
    
          break;
        }
      }
    
      if (!doesUserExists) {
        this.speakers.push(newSpeaker);
      }
    
      this.event.user = ''
    },
    
    Reply
  2. you can use findIndex

    addSpeaker() {
      const newSpeaker = {
        id: this.event.user.uid,
        name: this.event.user.name
      };
      const check = this.speakers.findIndex(x => x.name == this.event.user.name) === -1;
      check && this.speakers.push(newSpeaker);
      this.event.user = "";
    }
    
    Reply
  3. It is perfect time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or advice. Maybe you can write next articles referring to this article. I want to read even more things about it!

    Reply
  4. We wish to thank you again for the gorgeous ideas you gave Jesse when preparing her post-graduate research and, most importantly, regarding providing all of the ideas in one blog post. Provided that we had been aware of your blog a year ago, i’d have been saved the needless measures we were having to take. Thank you very much.

    Reply
  5. It’s the best time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or tips. Maybe you can write next articles referring to this article. I want to read even more things about it!

    Reply
  6. It’s the best time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or tips. Perhaps you can write next articles referring to this article. I desire to read more things about it!

    Reply
  7. Right here is the right blog for anybody who wants to understand this topic. You realize a whole lot its almost hard to argue with you (not that I personally would want to…HaHa). You certainly put a new spin on a subject that’s been written about for a long time. Wonderful stuff, just excellent!

    Reply
  8. Heya i am for the primary time here. I came across this board and I to find It truly helpful & it helped me out much. I’m hoping to offer one thing again and aid others like you aided me.

    Reply
  9. We would like to thank you once more for the wonderful ideas you offered Jesse when preparing her own post-graduate research as well as, most importantly, for providing the many ideas in a blog post. Provided we had known of your website a year ago, i’d have been rescued from the pointless measures we were employing. Thanks to you.

    Reply
  10. Right here is the right web site for anyone who would like to find out about this topic. You understand a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You definitely put a new spin on a topic that has been written about for ages. Great stuff, just excellent!

    Reply

Leave a Comment