javascript - 具有来自firebase可调用函数(gaxios)的异步状态值的Vuex getter
问题描述
我使用了几种技术的组合:Vue、Vuex、Vuetify、Google Firebase 可调用函数。此代码工作正常,并在使用我自己的 firebase 可调用函数getBatchData()异步加载数据后更新组件,但在从 firebase 接收数据之前,它会在控制台中显示错误:
[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘位置’”
我的问题是下一个 -如何在异步加载实际数据之前显示一些值?
//Vuex 动作(actions.js 文件):
import * as EVENT from './mutation-types.js';
import firebase from "firebase/app";
const getBatchData = firebase.functions().httpsCallable('getBatchData');
function convertProjectSummary(values) {
let projects = [];
const projectNameCol = 0;
const locationCol = 1;
const len = values.length;
for (var i = 1; i < len; i++) {
let record = {};
record.project = values[i][projectNameCol];
record.location = values[i][locationCol];
projects.push(record);
}
return projects;
}
const actions = {
updateBatchData: (context) => {
context.commit(EVENT.SET_LOADING, {loading: true});
getBatchData().then(resp => {
let projectSummaryArray = [];
let projectSummarySheetName = 'Project Summary';
//separation ranges:
const valueRanges = JSON.parse(resp.data);
for (var i = 0; i < valueRanges.length; i++) {
if (range.includes(projectSummarySheetName)) {
projectSummaryArray = valueRanges[i].values;
continue;
};
};
let projectSummaryRecords = convertProjectSummary(projectSummaryArray);
//=> state.projectRecords:[],
context.commit(EVENT.UPDATE_PROJECT_SUMMARY, {projectRecords: projectSummaryRecords});
context.commit(EVENT.SET_LOADING, {loading: false});
}).catch(error => console.log(error))
}
}
export default actions
//Vuex getter(getters.js 文件):
const getters = {
//getter function with parameter:
getProjectInfo: state => {
const records = state.projectRecords;
function searchProjectDate(projectName) {
if (records.length > 0) {
for(var i=0; i<records.length; i++) {
if(records[i].project == projectName) {
return records[i];
}
}
} /* not updated if use the next default value:
else {
return {
project: "data not available",
location: "data not available",
};
}
*/
}
//return a closure function
return searchProjectDate;
},
}
export default getters
//Vue projectInfo.vue 文件:
<template>
<!-- asynchronous computed value -->
<div><p :text="info.location"></p></div>
</template>
<script>
import { mapState, mapGetters} from "vuex";
export default {
name: "prj-info",
computed: {
...mapState(["currentProject"]),
...mapGetters(["getProjectInfo"]),
info() {
//getter function with parameter:
return this.getProjectInfo(this.currentProject);
}
}
}
</script>
我的错:相反,我提供的代码使用了一个子组件,但其中有一个错误:
<template>
<!-- asynchronous computed value -->
<div><GreyTextField :text="summary && summary.location" /></div>
</template>
<template>
<div class="grey-textfield">
{{theText}}
</div>
</template>
<script>
export default {
name: "grey-textfield",
props: {
text: {
type: String,
default: "Text"
},
},
data() {
return {
theText: this.text
}
},
watch: {
text: function(val, oldVal) {
this.theText = val;
},
},
}
</script>
我为文本属性添加了观察者。现在它可以工作了。对不起。有时共享代码以查找错误是件好事。
解决方案
推荐阅读
- matlab - 如何使用integral2来评估(显然)非向量化函数的积分?
- c# - Asp.Net Core 2.0 HTTP -> Kestrel 上的 HTTPS
- excel - Excel 公式 - 如果 A=1 B=2.......Z=26。如果你在单元格中输入 CAT 它应该显示结果 24
- javascript - 尝试动态创建删除按钮并在javascript中调用删除函数
- javascript - 两个动态呈现但共享相同路由的页面模板
- excel - 关闭表单时如何保存列表框值(多选)?
- c++ - 'strtoll' 和许多其他“不是 'std' 的成员”
- powershell - 无法将非单体脚本模块发布到具有 7 个或更多功能的本地 PowerShell 库
- vue.js - Nuxt.js - 尝试在 asyncData 方法中的失败等待调用中抛出 404 时出现“未定义错误”
- plsql - 如何找出 DBF 文件或任何文件的格式?