【发布时间】:2020-07-26 16:32:19
【问题描述】:
我今天可能已经盯着这个太久了。我有一个云功能,可以自动为上传的图像生成缩略图。上传缩略图后,我想要第二个函数来获取缩略图的 url 和获取原始对象的 url,以便将它们保存到文档中。
当我尝试部署时,我收到以下错误: src/index.ts:28:29 - 错误 TS2339:存储类型上不存在属性“ref”。
现在消息很清楚,但我真的不知道应该改用什么。我在网上看到的大部分内容都将 ref() 显示为属性。任何帮助将不胜感激!
这个问题在下面的 //get old 部分。
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
const firebase = admin.initializeApp();
export const addNewImageToPlantFile = functions.storage.bucket("users/{userID}/plants/{docID}/images").object().onFinalize(async (object, context) => {
//***ON CREATION OF NEW THUMBNAIL, WRITE TO FILE
//check image name to decide if run
if (object && object.name && object.name.endsWith('_200x200.jpg')) {
const {docID} = context.params;
//get the path of the original image
const split = object?.name.split('_200x200');
const pathFull = split.join();
console.log(pathFull);
//get the url
const urlThumb = object.mediaLink;
console.log(urlThumb);
//get the old
const storage = firebase.storage;
**const reference = storage.ref();**
const imageReference = reference.child(pathFull);
const urlFull = await imageReference.getDownloadURL();
console.log(urlFull);
//*****PACK THE DATA AND UPDATE FILE*****//
//get the time from ID
const time = Number(docID.split('_')[0]);
//pack the data
const data = {
'date': time,
'full': urlFull,
'thumb':urlThumb
};
//update document
return admin.firestore().doc('plants/' + docID).update({
imageSets: admin.firestore.FieldValue.arrayUnion(data)
});
} else {
console.log('Uploaded image is not a thumbnail (_200x200.jpg)');
return
}
//FUNCTION END
});
【问题讨论】:
标签: typescript firebase google-cloud-functions firebase-storage