简体   繁体   中英

Firebase Cloud Storage Upload File Path Error

I have a node.js app with Firebase Functions and using this to upload an image that is getting downloaded from another.network call and stored locally.

I'm getting the following error when trying to upload an image file to storage.

It's coming from the Firebase storage node modules with this function:

function _getChild$1(ref, childPath) {
    const newPath = child(ref._location.path, childPath);
    const location = new Location(ref._location.bucket, newPath);
    return new Reference(ref.storage, location);
}

I believe the ref is referring to storage in my upload function const storageRef = ref(storage, 'images');

ERROR:

const newPath = child(ref._location.path, childPath);  
TypeError: Cannot read properties of undefined (reading 'path')

CODE:

const { ref, uploadBytes } = require('firebase/storage');
const { initializeApp } = require('firebase-admin/app');
const { getStorage } = require('firebase-admin/storage');

firebaseConfig = {...}
const firebaseApp = initializeApp(firebaseConfig);
const storage = getStorage(firebaseApp);

const uploadImagesToFirebase = async () => {
    const storageRef = ref(storage, 'images');

    try {
        const upload = await uploadBytes(
            storageRef,
            '/Users/donovancotter/Desktop/Projects/image-uploaded/functions/public/37f8eb5d-8b42-45ed-ba3b-c7d35a299a62-0-1111264718.png'
        );
        return upload;
    } catch (e) {
        console.log(e);
    }
};

As you are using node import syntax, import the ref and uploadBytes functions with the import statement from the Firebase Storage client library.

You can check the below sample code for reference.Also check this document

import { getStorage, ref, uploadBytes } from "firebase/storage";
const storage = getStorage();
const uploadImagesToFirebase = async () => {
    const storageRef = ref(storage, 'images/profile')
    try {
        const upload = await uploadBytes(
            storageRef,
           file //This will be your user selected file from file explorer
        );
        return upload;
    } catch (e) {
        console.log(e);
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM