简体   繁体   English

如何在 axios 获得的对象数组中搜索 id? Vue 2

[英]How to search an array of objects obtained by axios for an id? Vue 2

I am trying to verify if the user is inside that list that I capture by axios, the issue is that I have used the FILTER option but it always returns undefined or [], being that if the user exists in that array.我正在尝试验证用户是否在我通过 axios 捕获的列表中,问题是我使用了 FILTER 选项,但它总是返回未定义或 [],即如果用户存在于该数组中。

I can't think what else to do, because I validate if it is by console.log() the variable with which I ask and if it brings data.我想不出还能做什么,因为我通过 console.log() 验证它是否是我询问的变量以及它是否带来数据。

    created() {
        this.getStagesDefault()
        this.getSalesman()
        this.getStagesAmountByUser()
    },
    methods: {
        async getSalesman(){
            const { data } = await axios.get('salesman')
            this.employees = data.data
        },
        getStagesAmountByUser(){
            console.log(this.user['id'])
            var objectUser = this.employees.filter(elem => {
                return elem.id === this.user['id']
            })

            console.log(objectUser)
        },

Console安慰

在此处输入图像描述

Vue data数据

在此处输入图像描述

getSalesman is an async method. getSalesman是一个异步方法。 At the time of the filter, the array being filtered is still empty.在过滤时,被过滤的数组仍然是空的。

this.getSalesman()            // this runs later
this.getStagesAmountByUser()  // this runs right away

Have the methods run sequentially by awaiting the async method:通过等待异步方法让方法按顺序运行:

await this.getSalesman()
this.getStagesAmountByUser()

The method getSalesman is asynchronous. getSalesman 方法是异步的。 Meaning that "getStagesAmountByUser" will start executing before "getSalesman" finishes.这意味着“getStagesAmountByUser”将在“getSalesman”完成之前开始执行。

2 ways to fix the problem:解决问题的2种方法:

  1. await the "getSalesman" method, but you have to make the created method async as well.等待“getSalesman”方法,但您还必须使创建的方法异步。 Making the code as follows:制作代码如下:
 created: async() =>{ this.getStagesDefault() this.getSalesman() this.getStagesAmountByUser() }
  1. Attach a.then to the getSalesman function, and start the next one inside the.then.将a.then附加到getSalesman function,然后在.then中启动下一个。 Making the code as follows:制作代码如下:
 created(){ this.getStagesDefault() this.getSalesman().then( () => this.getStagesAmountByUser()) }

You can avoid the inefficient clientside filtering if you pass the id to the backend and only select by that id.如果您将 id 传递给后端并且仅通过该 id 传递 select,则可以避免低效的客户端过滤。

Additionally, created only gets called once unless you destroy the component which is also inefficient, so watch when user.id changes then call your method again.此外,除非您销毁同样低效的组件,否则 created 只会被调用一次,因此请注意user.id何时更改然后再次调用您的方法。

Plus don't forget you must wrap any async code in a try/catch else you will get uncaught errors when a user/salesman is not found etc, you can replace console.error then with something which tells the user the error.另外不要忘记您必须将任何异步代码包装在 try/catch 中,否则当找不到用户/推销员等时您将收到未捕获的错误等,您可以将 console.error 替换为告诉用户错误的内容。

 { data: () => ({ employee: {} }), watch: { 'user.id' (v) { if (v) this.getEmployee() } }, created() { this.getEmployee() }, methods: { getEmployee() { if (typeof this.user.id === 'undefined') { return } try { const { data } = await axios.get(`salesman/${this.user.id}`) this.employee = data.data } catch (e) { console.error(e) } } } }

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

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