gpt4 book ai didi

javascript - 从 (click)= 调用多个函数

转载 作者:行者123 更新时间:2023-11-28 14:14:55 26 4
gpt4 key购买 nike

在从我的网站删除用户的情况下,我必须进行多个数据库调用才能删除 UserID 所在的所有选项卡(大约 10 个不同的选项卡)中的用户 ID。

现在我正在使用我认为是肮脏的黑客,我在同一个按钮上有多个 (click)="function()"事件触发多个函数,所有这些函数都从这里进入我的服务层并使得数据库基础。在我的服务中,我为每个必须进入并删除用户 ID 的表格提供了一个功能。

HTML:

<button (click)="deleteCandidateInCompanies(Item.owner)"(click)="deleteCandidateInOrganizations(Item.owner)"

组件.ts:

 deleteCandidateInCompanies(owner: string): void {
this._candidateService.deleteCandidateFromCompanies(id)
.then( data => {
console.log(data);
}).catch( err => {
alert('An error has occurred');
console.log(err);
});
}

deleteCandidateInOrganizations(owner: string): void {
this._candidateService.deleteCandidatesFromOrganizations(id)
.then( data => {
console.log(data);
}).catch( err => {
alert('An error has occurred');
console.log(err);
});
}

服务.ts:

deleteCandidateFromCompanies(owner: string) {
// Get User object
return new Promise(async (resolve, reject) => {

const _dynamoDB = new AWS.DynamoDB.DocumentClient();
const getUserParams = {
TableName: 'user-table',
Key: {
sub: owner
}
};
const user = await _dynamoDB.get(getUserParams).promise();

// Get all companies owned by user
for (let i = 0; i < user.Item.companies.length; i++) {
const companyUUID = user.Item.companies[i];
const getCompanyParams = {
TableName: 'company-table',
Key: {
uuid: companyID
}
};

const company = await _dynamoDB.get(getCompanyParams).promise();

// Check what index owner is on
const companyIndex = company.Item.owners.indexOf(owner);

if (companyIndex === -1) {
continue;
}

const deleteUserReferenceInCompanyParams = {
TableName: 'company-tabel',
Key: {uuid: user.Item.companies[i]},
UpdateExpression: 'REMOVE owners[' + i.toString() + ']'
};


_dynamoDB.update(deleteUserReferenceInCompanyParams, function (err, data) {
if (err) {
console.log(err);
reject(err);
} else {
console.log(data);
resolve(data);
}
});
}
});



deleteCandidatesFromOrganizations(owner: string) {
// Get User object
return new Promise(async (resolve, reject) =>{
const _dynamoDB = new AWS.DynamoDB.DocumentClient();
const getUserParams = {
TableName: 'user-table',
Key: {
sub: owner
}
};
const user = await _dynamoDB.get(getUserParams).promise();

// Get all organizations owned by user
for (let i = 0; i < user.Item.ownerOrganization.length; i++){
const organizationUUID = user.Item.ownerOrganization[i];
const getOrganizationParams = {
TableName: 'organization-table',
Key: {
uuid: organizationUUID
}
};

const organization = await
_dynamoDB.get(getOrganizationParams).promise();

// Tjekker hvilket index owner er på

const organizationsIndex = organization.Item.owners.indexOf(owner);

if (organizationsIndex === -1) {
continue;
}

const deleteUserReferenceInOrganizationParams = {
TableName: 'organization-table',
Key: {uuid: user.Item.ownerOrganization[i]},
UpdateExpression: 'REMOVE owners[' + i.toString() + ']'
};

// tslint:disable-next-line:typedef
_dynamoDB.update(deleteUserReferenceInOrganizationParams, function (err, data) {
if (err) {
console.log(err);
reject(err);
} else {
console.log(data);
resolve(data);
}
});
}
});
}

}

当我尝试将我的函数放入 óne 函数中的 component.ts 中时,我收到错误 Promies returned from deleteCandidateFromCompanies isignored。

我希望应该有一种不同的方式,将所有这些功能组合在一起,并获得更少的代码和数据库调用。

最佳答案

您可以采取的一种方法是,创建一个同时扭曲这两个函数的函数,并且您可以使用 async/await 来处理异步调用,例如:

async myFunctionWrapper(owner){
await deleteCandidateInCompanies(owner);
await deleteCandidateInOrganizations(owner);
}

并在 HTML 中调用包装器

<button (click)="myFunctionWrapper(Item.owner)" />

关于javascript - 从 (click)= 调用多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com