我创建了一个更改用户的功能,但是在使用此功能发出HTTP请求后,有时要花点时间才能获得返回值。
该函数成功运行,没有任何错误,但是问题是延迟了执行返回。
我注意到这种情况并不总是发生,在第一次调用后,该函数会快速运行...对此有解释吗?
函数执行耗时8617毫秒,状态码为200
函数执行耗时1182毫秒,状态码为200
函数执行耗时1361毫秒,状态码为200
......
auth.functions.ts
export const updateUserHttpRequest = functions.https.onRequest(async (req, res) => {
cors(req, res, () => {
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "PUT");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Access-Control-Allow-Headers", "Authorization");
switch (req.method) {
case 'PUT': {
admin.auth().updateUser(req.body.id, {
displayName: req.body.name,
photoURL: req.body.avatar,
email: req.body.email,
password: req.body.password,
}).then(async (authUserChanged) => {
try {
const databaseConfig = await firestore.collection(req.body.id).doc('database').get();
firestore.collection(req.body.id).doc('database').get();
const databaseUrl = databaseConfig.data().url;
const pathUsers = `${databaseUrl}/config/users`;
await firestore.collection(pathUsers)
.doc(req.body.id).update(req.body);
res.status(200).send({
message: 'User changed success',
authUser: authUserChanged
});
console.info(`User: ${authUserChanged.displayName}
changed success!`, authUserChanged.toJSON());
} catch (error) {
console.log(error);
res.status(500).send(error);
}
})
.catch(error => {
console.warn(error.errorInfo);
res.status(500).send(error.errorInfo);
console.log('Error on update user', error);
});
}
break;
default:
res.status(405).send({
error: 'Error, method not exist.'
});
break;
}
});
});
users.effects.ts:
@Effect()
updateUser$: Observable<Action> = this._actions$
.ofType(usersActions.ActionsTypes.UPDATE).pipe(
map((action: usersActions.Update) => action),
switchMap((data) => this.usersService.updateUser(data.payload).pipe(
map(() => {
return new usersActions.UpdateSuccess();
}),
catchError((res => {
return of(new usersActions.UpdateError({ error: res.error }));
}))
)
));
users.service.ts:
updateUser(user: any): Observable<ArrayBuffer> {
return this._http.put(this._requestUrl, user, this._httpOptions);
}