javascript - 如何调用数组中的变量
问题描述
我是 Node.js 和 Google Cloud Functions 的新手。将图像上传到 Firebase 存储后,我正在尝试生成缩略图,并且效果很好。但是有一个小问题我无法解决
const thumbarray = [];
exports.generateThumbnail = functions.storage.object('{pushId}/ProductImages').onFinalize((object, context) => {
// [START eventAttributes]
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
const fileDir = path.dirname(filePath);
const fileName0 = path.basename(filePath);
var fileName = fileName0.split("*")[1];
var thumbname = fileName0.split("*")[2]; // thumbnail 1 or 2 or 3
thumbarray.push(thumbname);
const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX}${fileName}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);
console.log("the file name is ", fileName);
console.log("this is the object ", object);
// [END eventAttributes]
// [START stopConditions]
// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
console.log('This is not an image.');
return null;
}
// Exit if the image is already a thumbnail.
if (fileName.startsWith(THUMB_PREFIX)) {
console.log('Already a Thumbnail.');
return null;
}
// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);
const metadata = {
contentType: contentType,
};
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
return file.download({
destination: tempLocalFile
});
}).then(() => {
console.log('The file has been downloaded to', tempLocalFile);
// Generate a thumbnail using ImageMagick.
return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {
capture: ['stdout', 'stderr']
});
}).then(() => {
console.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
return bucket.upload(tempLocalThumbFile, {
destination: thumbFilePath,
metadata: metadata
});
}).then(() => {
console.log('Thumbnail uploaded to Storage at', thumbFilePath);
// Once the image has been uploaded delete the local files to free up disk space.
fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);
// Get the Signed URLs for the thumbnail and original image.
const config = {
action: 'read',
expires: '03-01-2500',
};
return Promise.all([
thumbFile.getSignedUrl(config),
file.getSignedUrl(config),
]);
}).then((results) => {
console.log('Got Signed URLs.');
// var thumbname = fileName0.split("*")[2];
const thumbname = thumbarray[0];
console.log('the thumbname from array is ...', thumbname);
const thumbResult = results[0];
const originalResult = results[1];
const thumbFileUrl = thumbResult[0];
const fileUrl = originalResult[0];
// Add the URLs to the Database
return admin.database().ref('Listings/' + fileName).update({
path: fileUrl,
thumbname: thumbFileUrl // I want thumbname to be thumbname 1 or 2 or 3
});
}).then(() => console.log('Thumbnail URLs saved to database.'));
});
我在代码前面定义了 thumbname
var thumbname = fileName0.split("*")[2]; // thumbnail 1 or 2 or 3
我想稍后在此处的代码末尾调用它
thumbname: thumbFileUrl // I want thumbname to be thumbname 1 or 2 or 3
但它没有得到正确的变量(即缩略图1),而是将缩略图读取为字符串而不是变量。我怎样才能在这里调用变量?任何帮助都会得到帮助
解决方案
推荐阅读
- python - 根据条件从其他数据框中添加列
- javascript - 如何防止 TypeError 空白未定义?
- go - 未找到 Go Web 服务器 404 页面
- django - Django AttributeError:'str'对象没有属性'get'
- android - MTP 连接/通过 USB 电缆传输需要 Wifi/移动数据?
- python - 如何在不同的图中绘制两个结果?
- reactjs - 从不同的文件扩展样式化组件
- swift - 如何将乘数应用于 var 底部约束
- reactjs - 在 Reactjs 中使用动态 CSS 类名
- java - 我的应用程序在模拟器(Pixel 2 Api 25)上运行,但不在 android 设备上运行(Andriod 版本 [9、8 等])