我在使用 Firebase 版本 9.1.2 时收到此错误无法读取未定义的属性“createUserWithEmailAndPassword”。我已阅读 Firebase 实现,因为我完全复制了我使用的版本的代码。
这是错误:
ERROR TypeError: Cannot read property 'createUserWithEmailAndPassword' of undefined, js engine: hermes
这是我的注册码:
import {ScrollView, StyleSheet, View} from 'react-native';
import {useState} from 'react';
import React from 'react';
import {Button, Gap, Header, Input, Loading} from '../../components';
import {colors, useForm} from '../../utils';
import {createUserWithEmailAndPassword, getAuth} from 'firebase/auth';
import {getDatabase, ref, set} from 'firebase/database';
import {showMessage} from 'react-native-flash-message';
import {fireApp} from '../../config';
export default function Register({navigation}) {
const [form, setForm] = useForm({
fullName: '',
job: '',
email: '',
pass: '',
});
const [loading, setLoading] = useState(false);
const onContinue = () => {
console.log(form);
setLoading(true);
createUserWithEmailAndPassword(getAuth(fireApp), form.email, form.pass)
.then(success => {
setLoading(false);
const data = {
fullName: form.fullName,
job: form.job,
email: form.email,
};
set(ref(getDatabase(), 'users/' + success.user.uid + '/'), data);
})
.catch(error => {
const errorMessage = error.message;
setLoading(false);
showMessage({
message: errorMessage,
backgroundColor: colors.error,
color: colors.white,
});
console.log('register error : ', errorMessage);
});
我不明白这个问题。有什么建议吗?
尝试使用这个:
在函数
onContinue
中初始化身份验证,如下所示:
const auth = getAuth();
然后在
auth
中调用 createUserWithEmailAndPassword
,如下所示:
createUserWithEmailAndPassword(auth, form.email, form.pass)
这一切看起来像这样:
const onContinue = () => {
...
...
const auth = getAuth();
createUserWithEmailAndPassword(auth, form.email, form.pass)
...
...
...
};
这一直对我有用。
我从这里
得到了参考