【发布时间】:2018-08-13 12:58:57
【问题描述】:
我是一名 Android 开发人员,几乎没有网络经验。我写了一个云函数来注册用户。但是太嵌套了。我知道我可以使用承诺链或异步/等待。当我尝试使用异步 vs 代码时,会出现错误,它cannot find name, async,目标是 ES6。当我尝试链接承诺时,它会发出警告,例如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.`
}
);
});
}
});
}
});
});
这是我在更改为承诺链接时尝试过的,但显示警告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 的 Promise 链,使其更具可读性。
【问题讨论】:
-
我在您的代码中没有看到任何 async/await。
-
我把它恢复到这个状态。此代码按预期工作,但它过于嵌套。这就是为什么我想重构它以使用 async/await 或 Promise 链接
-
所以您是说您希望其他人为您完成这项工作,而不是为您自己可能做错的事情寻求帮助?
-
我只是在寻求帮助,我可以发布我写的带有所有这些错误的承诺链函数,但这也有点像我已经发布的函数,它也不起作用。这就是为什么我没有在这个问题中包含它
-
学习如何解释警告和错误非常有帮助,所以下次遇到它,你可以自己修复它。我怀疑通过了解自己的工作出了什么问题会更好地帮助您,这将有助于其他阅读此处遇到相同错误消息的人,这应该很容易搜索。
标签: typescript firebase async-await google-cloud-functions es6-promise