ChartJS 创建挂接时出错(承诺/异步):“类型错误:无法读取未定义”的属性'position'

jum4pzuy  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(130)

我正在使用Chartjs创建一个图表,该图表显示数据库中存在的信息,尽管显示了信息,但我一直收到以下错误:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in created hook (Promise/async): "TypeError: Cannot read property 'position' of undefined"
    found in
    ---> <Reports> at src/views/Reports.vue
           <VMain>
             <VApp>
               <Navbar> at src/layout/Navbar.vue
                 <App> at src/App.vue
                   <Root>

还有这个:

TypeError: Cannot read property 'position' of undefined
        at _callee$ (Reports.vue?0011:53)
        at tryCatch (runtime.js?96cf:63)
        at Generator.invoke [as _invoke] (runtime.js?96cf:294)
        at Generator.eval [as next] (runtime.js?96cf:119)
        at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
        at _next (asyncToGenerator.js?1da1:25)

该错误将我指向此函数,在此函数中,我将信息传递给图表

async created() {
        const {
            data
        } = await axios.get("http://localhost:4000/users");
        var c = 0
        for (var i = 0; i < 1000; i++) {
            if (data[i].position in this.labels) {
                continue
            } else {
                this.labels.push(data[i].position)
                this.confirmed.push(data[i].id)
                c = c + 1
                if (c == 28) {
                    break
                }
            }
        }
        console.log(this.labels)
    }

注:我的数据库包括列位置、ID和名称。

kyxcudwk

kyxcudwk1#

是否确定const { data } = ...正确?axios.get返回解析为{ response: { data: {}}而不是{ data: {} }的Promise

evrscar2

evrscar22#

如果使用created、fetch或any,则应将其包含在if语句中,如下所示:

if(process.client){
   //your code to fetch data from the server.
}

我希望它也能解决你的问题。

相关问题