如何从Firebase获取要放入数据库的图像的URL路径?

时间:2019-02-19 14:40:10

标签: javascript reactjs firebase firebase-storage redux-saga

如何获取图像的URL放入数据库?我可以正确创建,但无法获取URL。我的代码如下:

export  function* createTest(database, storage, action){
    try{
        const {testName, date, image} = action.test

        const ref = yield storage.ref(`images/${testName}`)
        const imagePath = yield new Promise(resolve =>{
            resolve(ref.put(image[0]))
        })
        console.log(imagePath.metadata.downloadURLs[0]) //return undefined
        const id = yield database.ref().child(`some/${action.someId}/tests}`).push().key    
        const newpath = `some/${action.someId}/test/${id}`
        const test = {
            testName,
            imagePath.metadata.downloadURLs[0],
            date
        }
        yield database.ref(newpath).update(test)
        yield put(ActionCreator.createTestSuccess(test))
    }catch({message}){
        yield put(ActionCreator.createTestFailure(message))
    }
    }

1 个答案:

答案 0 :(得分:0)

var ref = storage.ref(`images/${testName}`);

var imageUrl = '';
// Get the download URL
ref.getDownloadURL().then(function(url) {
// Insert url into an <img> tag to "download"
    imageUrl = url;
});