【发布时间】:2021-04-27 06:16:37
【问题描述】:
我正在启动一项云功能,以便复制我在 Firestore 中的一个寄存器。其中一个字段是图像,该函数首先尝试复制图像,然后复制寄存器。
这是代码:
export async function copyContentFunction(data: any, context: any): Promise<String> {
if (!context.auth || !context.auth.token.isAdmin) {
throw new functions.https.HttpsError('unauthenticated', 'Auth error.');
}
const id = data.id;
const originalImage = data.originalImage;
const copy = data.copy;
if (id === null || originalImage === null || copy === null) {
throw new functions.https.HttpsError('invalid-argument', 'Missing mandatory parameters.');
}
console.log(`id: ${id}, original image: ${originalImage}`);
try {
// Copy the image
await admin.storage().bucket('content').file(originalImage).copy(
admin.storage().bucket('content').file(id)
);
// Create new content
const ref = admin.firestore().collection('content').doc(id);
await ref.set(copy);
return 'ok';
} catch {
throw new functions.https.HttpsError('internal', 'Internal error.');
}
}
我尝试了多种组合,但此代码总是失败。由于某种原因,复制图像的过程失败了,我做错了什么?
谢谢。
【问题讨论】:
-
copy中的await ref.set(copy);是什么?另外,您能否分享一下您的 Cloud Function 的整个代码以及您遇到的错误? -
@RenaudTarnec 我已经用完整的功能代码更新了这个问题。 'copy' 是要添加到 firestore 的文档。
-
你能在 catch 块中记录错误吗?
catch(error) {…}。另外,content存储桶是否存在? -
既然你提到了它,我不明白存储桶是如何工作的。我已经设置了正确的存储桶名称,一切正常,谢谢。
标签: javascript firebase google-cloud-functions google-cloud-storage firebase-storage