【发布时间】:2021-11-03 23:47:41
【问题描述】:
以下是我从表单中添加数据的代码,当它会捕获 this.error 的值时,该变量没有给出任何值。我的代码行有问题吗?
saveContact: async function() {
this.errors = [];
if (this.name && this.phone) {
this.loading = !this.loading;
await addDoc(collection(db, "users"), {
id: this.id,
name: this.name,
phone: this.phone
}).then(() => {
this.name = ""
this.phone = ""
// This empty errors not working
this.errors = [];
}).catch((error) => {
this.error = error
});
}
if (!this.name) this.errors.push("name");
if (!this.phone) this.errors.push("phone");
}
this.error先用来容纳会发生的错误,然后如果firebase中的addDoc()函数成功了,then(),this.error = []变量被清空,这样错误就消失了,但是确实不工作。
【问题讨论】:
-
你能解释一下预期的行为和正在发生的事情吗?
-
“不工作” - 你怎么知道?你在函数顶部设置了
this.errors = [],并在.then中做同样的事情……你怎么知道它“不工作”? -
我想清空 'name' 值,但是当我设置 this.name = "" 时,this.errors 会给出错误符号。所以我想要this.error的空数组。但不工作,而 this.name 工作
-
@bravo this.errors = [] 用于清除每个运行的函数的所有错误。在我设置 this.name = '' 之后,然后在 then() 内部是干净的错误,但它不起作用。
-
它正在工作,但是你在函数的底部添加它!因为当代码运行时 this.name 和 this.phone 现在将为空
标签: javascript firebase vue.js google-cloud-firestore