[英]firebase.database.ref is not a function React Native / Expo
I am pretty new to expo and firebase, and I have this error that I have no idea what the issue is.我对 expo 和 firebase 很陌生,我有这个错误,我不知道问题是什么。 I am trying to fetch photos from firebase database.
我正在尝试从 firebase 数据库中获取照片。 I know for sure the issue is not with firebase config because I can upload photos into firebase storage.
我确定问题不在于 firebase 配置,因为我可以将照片上传到 firebase 存储中。
I suspect the issue is with exporting and importing firebase.我怀疑问题出在导出和导入 firebase 上。
This is the error message I am getting:这是我收到的错误消息:
[TypeError: _firebase.db.ref is not a function.
[类型错误:_firebase.db.ref 不是 function。 (In '_firebase.db.ref('users/photos')', '_firebase.db.ref' is undefined)]
(在 '_firebase.db.ref('users/photos')' 中,'_firebase.db.ref' 未定义)]
Note: I am using firebase v9注意:我使用的是 firebase v9
App.js file: App.js 文件:
import { db } from './firebase';
export default function App() {
async function loadPhotos() {
try {
db.ref('users/photos')
.then(url => {
console.log('URL: ', url);
})
.catch(e => console.log(e));
console.log('Got here');
} catch (error) {
console.log('error', error);
}
}
...............
}
firebase.js file: firebase.js 文件:
import firebase from 'firebase/compat/app';
import { getDatabase } from 'firebase/database';
const firebaseConfig = {
apiKey: '......',
authDomain: '.....',
projectId: '....',
storageBucket: '....',
messagingSenderId: '....',
appId: '.....',
measurementId: '....',
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
export const db = getDatabase();
In v9 and later of the Firebase SDK, most functionality that was a method on the objects in the past, is now available as a top-level function.在 Firebase SDK v9 及更高版本中,过去作为对象方法的大多数功能现在可作为顶级 function 使用。
So instead of db.ref('users/photos')
, you need to do ref(db, 'users/photos')
.因此,您需要执行
ref(db, 'users/photos')
而不是db.ref('users/photos')
photos') 。
You're also missing a get
call, which is how you actually retrieve the data from the reference:您还缺少一个
get
调用,这是您实际从引用中检索数据的方式:
get(ref(db, 'users/photos'))
.then(url => {
console.log('URL: ', url);
})
.catch(e => console.log(e));
This is all pretty well documented in the Firebase documentation on reading data , so I recommend keeping that handy.这在 Firebase 关于读取数据的文档中都有很好的记录,所以我建议把它放在手边。 Alternatively you can use the
compat
paths in v9, to make the older syntax work as shown in the upgrade guide /或者,您可以使用 v9 中的
compat
路径,以使旧语法如升级指南中所示工作 /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.