我正在尝试从 Firebase 云函数访问我的存储
但我无法访问它
它给我
类型错误:storage.ref 不是一个函数
这是我的代码
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var db = admin.firestore();
var storage = admin.storage().bucket();
和我的功能体
exports.deletingVisaCop = functions.firestore.document('users/{user_Id}/info/visa_cop').onUpdate((change,context) =>{
var userId = context.params.user_Id;
// var userRef = db.collection("users").doc(userId).collection("info").doc("visa_cop");
var data = change.after.data().state;
var deleteData = setInterval(function () {
var visaCopStorageRef = storage.ref();
var ref = visaCopStorageRef.child('visa_cop');
ref.delete().then(function(){
console.log("Deleted")
clearTimeout(deleteData);
return true;
}).catch(function(error){
console.log("errorIs" + error)
})
}, 1000);
if(data === true)
{
return deleteData;
} else
{
return clearTimeout(deleteData);
}
});
我希望当计时器结束时..存储中的节点删除
谢谢
更新
新错误
Error { ApiError: Not Found
at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:193:30)
at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:131:18)
at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:496:12
at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/node_modules/retry-request/index.js:198:7)
at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:185:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1161:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
code: 404,
errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found' } ],
response: undefined,
message: 'Not Found' }
使用var storage = admin.storage().bucket();
,您将默认存储桶分配给storage
变量,如文档中所述。
如此处所述,Bucket
没有任何ref()
方法,因此您在执行storage.ref();
时会遇到错误。
如果要删除存储桶,只需使用其delete()
方法,如下所示。
如果要删除另一个存储桶,即"visa_cop"存储桶,只需按如下方式声明它,然后使用 delete()
方法:
const visaCopBucket = admin.storage().bucket('visa_cop');
return visaCopBucket.delete()
.then(function(data) {
....
});
<小时 />请注意,目前还不清楚为什么在云函数中引入"计时器"。功能目标是什么?
您的代码看起来不正确。Firebase 文档建议执行以下操作:
// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();
// Create a storage reference from our storage service
var storageRef = storage.ref();