[英]Refactoring typescript cloud function to use promise chaining
我是一位Android开发人员,几乎没有网络经验。 我写了一个云功能来注册用户。 但是太嵌套了。 我知道我可以使用诺言链或异步/等待。 当我尝试使用异步vs代码给出错误消息时,它cannot find name, async
,目标是ES6。 当我尝试链接promises时,它发出警告,例如, not all code paths returns a value
。 这是我的代码
exports.register = functions.https.onRequest((request, response) => {
const db = admin.firestore();
const user: string = request.body['username'];
const phone: number = request.body['phone'];
const password: string = request.body['password'];
return db.collection('rejectedContacts').where('contact', '==', phone).get()
.then(rejectedContactsSnapShot => {
if (rejectedContactsSnapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is blocked, please try again with another number`,
result: null
}
);
} else {
return db.collection('users').where('contacts.phone', '==', phone).get()
.then(contactsSnapShot => {
if (contactsSnapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is already assigned with an account. Did you forgot your pasword?`,
result: null
}
);
} else {
return db.collection('users').add(
{
user: user,
password: password,
isBlocked: false,
joiningDate: Date.now(),
phoneVerified: false,
deleted: false,
contacts:
{
phone: phone
}
}
).then((writeResult) => {
return response.json(
{
result: `User with ID: ${writeResult.id} added.`
}
);
});
}
});
}
});
});
这是我更改为Promise链接时尝试的方法,但显示警告, not all code paths return a value
exports.register = functions.https.onRequest((request, response) => {
const db = admin.firestore();
const user: string = request.body['username'];
const phone: number = request.body['phone'];
const password: string = request.body['password'];
return db.collection('rejectedContacts').where('contact', '==', phone).get()
.then(rejectedContactsSnapShot => {
if (rejectedContactsSnapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is blocked, please try again with another number`,
result: null
}
);
}
}).then(notRejected=>{
return db.collection('users').where('contacts.phone', '==', phone).get()
.then(contactsSnapShot => {
if (contactsSnapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is already assigned with an account. Did you forgot your pasword?`,
result: null
}
);
}
});
}).then(numberDoesNotExists=>{
return db.collection('users').add(
{
user: user,
password: password,
isBlocked: false,
joiningDate: Date.now(),
phoneVerified: false,
deleted: false,
contacts:
{
phone: phone
}
}
);
}).then((writeResult) => {
return response.json(
{
result: `User with ID: ${writeResult.id} added.`
}
);
});
});
谁能帮助我重构此代码以使用承诺链的异步/等待,以便使其更具可读性。
不知道您尝试了什么,我不确定为什么会首先出现错误,但是使用async / await进行代码的简单转换将是:
functions.https.onRequest(async (request, response) => {
const db = admin.firestore();
const user: string = request.body['username'];
const phone: number = request.body['phone'];
const password: string = request.body['password'];
let rejectedContactsSnapShot = await db.collection('rejectedContacts').where('contact', '==', phone).get();
if (rejectedContactsSnapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is blocked, please try again with another number`,
result: null
}
);
} else {
let contactsSnapShot = await db.collection('users').where('contacts.phone', '==', phone).get();
if (contactsSnapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is already assigned with an account. Did you forgot your pasword?`,
result: null
}
);
} else {
let writeResult = await db.collection('users').add({
user: user,
password: password,
isBlocked: false,
joiningDate: Date.now(),
phoneVerified: false,
deleted: false,
contacts:{
phone: phone
}
})
return response.json(
{
result: `User with ID: ${writeResult.id} added.`
}
);
}
}
});
注意您的代码是一个很大的问题,没有更多上下文,上面的代码可能包含错误,但这应该可以帮助您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.