javascript - 使用 onClick 事件处理程序时不断调用自身
问题描述
我期望发生的事情: 当用户单击 addProject 按钮时,事件侦听器将运行调用
formSubmit
我们将检查日期是否有效,然后如果有效,它将调用fetchingCompanyNameAndUserData
它将获取所需的数据更新状态,它会checkUniqueName
再次调用它将获取一些数据以确保没有重复,然后它应该调用this.insert()
最终将数据插入我们的firestore-NoSQL-DB。
问题:
这些功能特别是checkUniqueName
一遍又一遍地自我调用,我不知道那里有什么问题。编码:
formSubmit = event => {
event.preventDefault();
const isLoading = this.state;
var sdate = this.state.projectData.sdate;
var edate = this.state.projectData.edate;
if (sdate > edate) {
NotificationManager.error(`Please entre a valid dates`);
return;
} else {
// isLoading = true;
this.fetchingCompanyNameAndUserData();
}
};
fetchingCompanyNameAndUserData = async () => {
const userRef = fireStore.collection('users');
const userData = await userRef.where("Email", "==", auth.currentUser.email).get();
userData.forEach(doc => {
console.log('this one must match', doc.data().CompanyName)
const cashedFirstName = doc.data().FirstName;
const cashedLastName = doc.data().LastName;
const fullName = cashedFirstName + ' ' + cashedLastName;
return this.setState({
companyName: doc.data().CompanyName,
userName: fullName,
}, () => {
console.log('done fetching');
this.checkUniqueName();
});
})
};
checkUniqueName = async () => {
const projectName = this.state.projectData.title;
const companyName = this.state.companyName;
const projectRef = fireStore.collection('PROJECT')
const projectData = await projectRef.where("ProjectName", "==", projectName).get();
projectData.forEach(doc => {
if (doc.data().CompanyName !== companyName) {
console.log('checking unique nameing');
this.insert();
} else {
NotificationManager.error('this project already exists');
}
})
}
async insert() {
//async function foo() {
console.log('insreting proooo');
var ptitle = this.state.projectData.title;
var pdesc = this.state.projectData.desc;
var sdate = this.state.projectData.sdate;
var edate = this.state.projectData.edate;
var status = this.state.projectData.status;
var companyName = this.state.companyName;
try {
let response = await fireStore.collection("PROJECT").add(
{
ProjectName: ptitle,
CompanyName: companyName,
ProjectDescription: pdesc,
startDate: sdate,
EndDate: edate,
Status: status,
CreatedBy: auth.currentUser.email,
CreatedDate: toUTCDateString(new Date()),
LastModifiedBy: auth.currentUser.email,
LastModifiedDate: toUTCDateString(new Date()),
UserDocId: auth.currentUser.uid
});
let doc = await fireStore.collection("PROJECT").doc(response.id).get()
this.handleClose();
//alert(doc.id)
var d1 = doc.id;
this.props.history.push('/app/dashboard/addproject/' + d1);
//this.handleClose;
NotificationManager.success('Project Created Successfully!');
}
catch (error) {
//console.log('error: ', error);
console.log(error)
}
}
希望我在这里尽可能清楚
解决方案
您的checkUniqueName()
函数可以重写为:
checkUniqueName = async () => {
const projectName = this.state.projectData.title;
const companyName = this.state.companyName;
const projectRef = fireStore.collection('PROJECT')
const qsMatchingProjects = await projectRef.where("ProjectName", "==", projectName).where("CompanyName", "==", companyName).get();
if (qsMatchingProjects.empty) {
this.insert();
} else {
NotificationManager.error('this project already exists');
}
}
推荐阅读
- javascript - 复制浏览器 URL 栏的特定部分
- regex - 如何使用正则表达式从 vscode 用户片段中的 TM_FILEPATH 返回目录?
- compiler-construction - 证明不是 LALR(1) 的 LR(1) 文法必须只有 reduce/reduce 冲突
- ghost-blog - 如何在子目录上制作幽灵主页?
- c# - 为什么我在此代码的循环中出现错误?
- java - 从现有变量创建映射(例如,Java 等价于 JavaScript 的 `{varA, varB, varC}`)
- html - 在模板中动态加载背景图片
- r - 使用R中的函数和map()通过不同的列连接多个数据框?
- java - 如何改进解决缺失坐标的算法?
- javascript - 如何将视频背景对齐为居中而没有截止?