CloudFunctions中的FirebaseStorage:TypeError:storage.ref不是函数

时间:2019-01-13 16:18:36

标签: node.js firebase google-cloud-functions firebase-storage

我试图从Firebase云功能访问我的存储

但我无法访问

它给了我

TypeError: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' }

2 个答案:

答案 0 :(得分:1)

使用var storage = admin.storage().bucket();,您将为默认存储桶分配给storage变量,如文档中的here所述。

here所述,Bucket没有任何ref()方法,因此在进行storage.ref();时会遇到错误。

如果要删除存储桶,只需使用其delete()方法,如图here所示。

如果您要删除另一个存储桶(即“ visa_cop”存储桶),只需按以下说明进行声明,然后使用delete()方法:

const visaCopBucket = admin.storage().bucket('visa_cop');
return visaCopBucket.delete()
.then(function(data) {
   ....
});

请注意,为什么在Cloud Function中引入“计时器”尚不清楚。功能目标是什么?

答案 1 :(得分:0)

您的代码看起来不正确。 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();