大家好,我尝试通过功能将一些图像传递到我的Firebase,但是当我在终端内部署firebase(使用cli firebase deploy)时,出现错误,并且部署中止。 显然错误提示我在function / index.js内部的require不是一个函数(第9行第45行),但是gcconfig是使用适合我的正确方法执行的。 我想从CLI正确部署Firebase。
我在终端上遇到了来自cli firebase部署的错误:
Error: Error occurred while parsing your function triggers.
TypeError: require(...) is not a function
at Object.<anonymous> (/Users/luca/Code/Code_Taff/Benibla/bnblproto/functions/index.js:9:45)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Module.require (internal/modules/cjs/loader.js:643:17)
at require (internal/modules/cjs/helpers.js:22:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)
function / index.js:
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const fs = require('fs');
const UUID = require("uuid-v4");
const gcconfig = {
projectId: "beniblaproto",
keyFilename: "beniblaproto.json"
};
const gcs = require("@google-cloud/storage")(gcconfig);
exports.storeImage = functions.https.onRequest((request, response) => {
return cors(request, response, () => {
const body = JSON.parse(request.body);
fs.writeFileSync("/tmp/uploaded-image.jpg", body.image, "base64", err => {
console.log(err);
return response.status(500).json({error: err});
});
const bucket = gcs.bucket("beniblaproto.appspot.com");
const uuid = UUID();
return bucket.upload(
"/tmp/uploaded-image.jpg",
{
uploadType: "media",
destination: "/events/" + uuid + ".jpg",
metadata: {
metadata: {
contentType: "image.jpg",
firebaseStorageDownloadTokens: uuid
}
}
},
(err, file) => {
if (!err) {
response.status(201).json({
imageUrl: "https://firebasestorage.googleapis.con/v0/b" +
bucket.name +
"/o/" +
encodeURIComponent(file.name) +
"?alt=media&token=" +
uuid
});
} else {
console.log(err);
response.status(500).json({error: err});
}
}
);
});
});
如果有人可以向我解释我失败的地方和原因。 谢谢大家,祝你有美好的一天
答案 0 :(得分:0)
首先,您需要导入{Storage}库;
const {Storage} = require("@google-cloud/storage");
之后,您需要使用配置对象作为实例;
const gcs = new Storage(gcconfig);