Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
59 views
in Technique[技术] by (71.8m points)

javascript - Vue duplicate object

I have some strange problems with receiving Vue events. I have integrated laravel Echo with Pusher, and I have one laravel controller that insert data in database and run event. When I receive event on front-end, it is showing wrong data, actually always duplicate of the first object, and it's not showing it last, but first. When I refresh page everything looks fine. In Vue devtools says that the problem is with mutation ADD_SERVER, and in state 'adminServers' is duplicating object [0], but I can't figure where I have messed it up. I have allowed Pusher event log into console, and event is totally ok from backend side. So here is little code:

This are actions.js

  ADD_SERVER({commit}, server) {
    Api().post('/admin/server', server).then(res => {
        if (res.data === "Server Added")
          console.log('ADDED')
    }).catch(err => {
        console.log(err)
    })
  },

Those are getters.js

addServer: state => {
    return state.addServer
  },

  adminServers: state => {
      return state.adminServers
  },

Here is the mutation:

ADD_SERVER(state, server) {
    state.adminServers.unshift(server);
  },

The state.js

addServer               : {
          name: '',
          ip: '',
          password: '',
          user: '',
  },
  adminServers            : [],

And finally the main vue file:

<script>
import { mapGetters } from "vuex";
import Administration from "@/http/Administration";

export default {

  computed: {
            ...mapGetters(["adminServers"]),
            ...mapGetters(["addServer"]),
        },

        mounted() {
          window.Echo.channel("addServer").listen(".server-created", (e) => {
            this.$store.commit("ADD_SERVER", e.server);
          });
          this.$store.dispatch("GET_SERVERS");
    }
}

</script>

Is state.adminServers.unshift(server); wrong?

Keep in mind that I'm not adding events from AddServer.vue Component, I'm sending it from postman. ListServers is just listing.

question from:https://stackoverflow.com/questions/65621394/vue-duplicate-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...