我正在使用Vuejs和Firebase作为后端开发一个WebApp,您(无需身份验证)可以在其中构建玩具。要制作玩具,您可以选择3个不同的部分(头部,身体和脚),这些部分在以垂直列排列的3个光滑轮播中显示为图像。一旦选择了自己喜欢的设计,该应用就会询问您的电子邮件。
因此,我设法将所有订单数据存储在Firebase中(头部,身体部位,脚部,玩具名称,客户电子邮件,订单日期),当我的客户单击“购买”按钮时,我想触发包含所有信息的电子邮件,但特别,我需要在 Firebase Functions 中检索3张图片的下载URL,以便每当我的客户订购玩具时,他/她收到一封包含设计玩具图片的电子邮件。为此,我将SendGrid与Firebase一起使用。
我能够成功编写Firebase Functions,但是当我尝试弄乱存储时会遇到错误。这是我的代码:
1)Firebase功能
const functions = require('firebase-functions');
const storage = require('@google-cloud/storage');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
// Function that triggers an email when a new order is created in the database
exports.newOrderEmail = functions.database
.ref('/pedidos/{cleanEmail}/{newOrder}')
.onCreate((snapshot, context)=>{
const cleanEmail = context.params.cleanEmail;
// the new order
const newOrder = context.params.newOrder;
// mail of our customer
const customerEmail = cleanEmail.replace(/\,/g, '.');
const orderData = snapshot.val();
const head = orderData.cabeza;
const body = orderData.cuerpo;
const feet = orderData.patas;
const toyName = orderData.nombre;
const date = orderData.fecha_pedido;
// load Array of images
const toy_images = getAllURLImages(head, body, feet);
const msg = {
to: customerEmail,
from: 'ihojmanb@gmail.com',
subject: 'Tu monomono',
templateId: 'd-f884708acb5f45728e24d0d88442918c',
substitutionWrappers: ['{{', '}}'],
substitutions: {
toy_name: toyName,
head_part: toy_images[0],
body_part: toy_images[1],
feet_part: toy_images[2],
date: order_date,
}
};
return sgMail.send(msg)
});
Im使用的JavaScript方法(与Firebase函数在同一index.js文件中):
// return 3 images that we took from storage
function getAllURLImages(myHead, myBody, myFeet){
var head_image = getImageURL("img/head/", myHead);
var body_image = getImageURL("img/body/", myBody);
var feet_image = getImageURL("img/feet/", myFeet);
return [head_image, body_image, feet_image];
}
// get images from storage, by given path
function getImageURL(imageDir, myImagePath){
var image_path = imageDir + myImagePath + '.png';
console.log('image path: ' + image_path);
// var storage = admin.storage();
var storageRef = storage.ref();
var pathReference = storageRef.child(image_path);
pathReference.getDownloadURL().then(function(url) {
var image_url = url;
console.log('image url: ' + image_url);
return image_url;
}).catch(function(error) {
});
}
我在Firebase控制台上遇到的错误是:
TypeError: storage.ref is not a function
at getImageURL (/user_code/index.js:84:30)
at getAllURLImages (/user_code/index.js:72:22)
at exports.newOrderEmail.functions.database.ref.onCreate
(/user_code/index.js:48:29)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-
functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-
functions.js:135:20)
at /var/tmp/worker/worker.js:758:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
在我的应用程序的其他方法中,storage.ref没有问题 感谢您提供有关Firebase功能的任何建议,找不到解决我问题的正确答案。