我有一个应用程序,需要从阵列中删除UID。因为用户无权编写文档,所以我通过云功能进行更新。我的云功能如下:
async function deleteProjectUserKey(request): Promise<Responce> {
//console.log('deleteProjectUserKey() started');
try {
const uid = request.body.uid;
const projectKey = request.body.payload.projectKey;
const snapshot = await firestore.doc(`/${projectsPath}/${projectKey}`).get();
const project: Project = <Project>snapshot.data();
let success = false;
let idx = project.roUids.findIndex(_uid => _uid === uid);
if (idx >= 0) {
project.roUids.splice(idx, 1);
await firestore.doc(`/${projectsPath}/${projectKey}`).update({ 'roUids': project.roUids });
success = true;
} else {
idx = project.rwUids.findIndex(_uid => _uid === uid);
if (idx >= 0) {
project.rwUids.splice(idx, 1);
await firestore.doc(`/${projectsPath}/${projectKey}`).update({ 'rwUids': project.rwUids });
success = true;
}
}
if (success) {
return { status: 200, message: 'Successfully deleted project user key' };
} else {
return { status: 400, message: 'No souch project user key' };
}
}
catch (error) {
return { status: 400, message: error.message };
}
当我在运行该功能后在控制台中查看时,这似乎工作正常:
刷新浏览器后,我返回的数据正确无误:
读取客户端数据的功能如下:
getProjects(uid?: string): Observable<Project[]> {
const _uid = uid !== undefined ? uid : this.uid;
const rwUids$ = <Observable<Project[]>>this.db.collection(`${projectsPath}`, ref => ref.where('rwUids', 'array-contains', _uid))
.valueChanges();
const roUids$ = <Observable<Project[]>>this.db.collection(`${projectsPath}`, ref => ref.where('roUids', 'array-contains', _uid))
.valueChanges();
return combineLatest(roUids$, rwUids$)
.pipe(
map(([roUids, rwUids]) => [...roUids, ...rwUids]),
filter(projects => projects.length > 0),
tap(projects => this.debug('getProjects() emitted', 'projects', projects, 'project', this.dbData.project)),
);
}
因此,我的应用程序似乎无法自动反映云功能对阵列的更新。有人可以解释为什么吗?
编辑: 我试图按照@Renaud Tarnec的建议更改云功能:
async function deleteProjectUserKey(request): Promise<Responce> {
//console.log('deleteProjectUserKey() started');
try {
const uid = request.body.uid;
const projectKey = request.body.payload.projectKey;
const readOnlyArray = request.body.payload.readOnlyArray;
if (readOnlyArray) {
await firestore.doc(`/${projectsPath}/${projectKey}`).update({
roUids: admin.firestore.FieldValue.arrayRemove(uid)
});
} else {
await firestore.doc(`/${projectsPath}/${projectKey}`).update({
rwUids: admin.firestore.FieldValue.arrayRemove(uid)
});
}
return { status: 200, message: 'Successfully deleted project user key' };
}
catch (error) {
return { status: 400, message: error.message };
}
}
但是结果和以前完全一样:firestore db看起来正确,但是我返回的数组是错误的。