Javascript firebase存储无法读取未定义的属性'child'

问题描述 投票:0回答:1

您好,我目前正在按照docs从Firebase存储中获取图像,并根据登录的userId将其显示在我的html中

index.html:

<script src="scripts/config.js"></script>
<script src="scripts/auth.js"></script>

config.js:

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-storage.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-analytics.js"></script>
  var firebaseConfig = {
    apiKey: "xxxxxxxxxxx",
    authDomain: "quickstart-xxxxxxx.firebaseapp.com",
    databaseURL: "https://quickstart-xxxxxxx.firebaseio.com",
    projectId: "xxxxxxxx-xxxxx",
    storageBucket: "xxxx-xxxxxx.appspot.com",
    messagingSenderId: "xxxxxxxxx",
    appId: "1:xxxxxx:web:xxxxx",
    measurementId: "x-xxxxxx"
  };

firebase.initializeApp(firebaseConfig);
firebase.analytics();

const storage = firebase.storage();
const auth = firebase.auth();
const db = firebase.firestore();

auth.js:

console.log(`test/${user.uid}/profile-pic.jpg`);
    storage.storageRef.child(`test/${user.uid}/profilepic.jpg`).getDownloadURL().then(function(url) {
          $("#pic").attr("src", url);
    }).catch(function(error) {
          console.error(error);
    });

但是我当前遇到以下错误:

auth.js:11未捕获的TypeError:无法读取的属性'child'未定义

我确定通过控制台日志输入到child()中的参数具有一个值:

console.log(`test/${user.uid}/profile-pic.jpg`);

显示具有登录用户ID的当前字符串:

test / dtrNGt9usCSqwV86BfKnw6SjWHb2 / profile-pic.jpg

我仔细检查了一下文档,看我是否写错了什么,但似乎找不到任何东西。

我还检查了我的Firebase存储器上文件的路径,并且确实在测试文件夹中

enter image description here希望有人可以告诉我我的代码有什么问题

javascript firebase firebase-storage
1个回答
0
投票

哦,我发现我虽然storageRef是一个函数,但这是对该文件夹的引用。我将其更改为此,现在可以正常工作:

storage.ref(`test/${user.uid}/profilepic.jpg`).getDownloadURL().then(function(url) {
   $("#pic").attr("src", url);
}).catch(function(error) {
    console.error(error);
);
© www.soinside.com 2019 - 2024. All rights reserved.