Why might I be getting a: TypeError: updatePassword is not a function
Why might I be getting a: TypeError: updatePassword is not a function
我有一个带有更新配置文件页面的简单 React 应用程序,使用 firebase 进行身份验证,当我尝试更改密码时,我得到了这个 error:TypeError:updatePassword 不是一个函数。
这是我的UpdateProfile.js
import React, { useRef, useState } from "react";
import { Form, Button, Card, Alert } from "react-bootstrap";
import { getAuth } from "firebase/auth";
import { Link, useNavigate } from "react-router-dom";
export default function UpdateProfile() {
const emailRef = useRef();
const passwordRef = useRef();
const passwordConfirmRef = useRef();
const { currentUser, updateEmail, updatePassword } = getAuth();
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const navigate = useNavigate();
function handleSubmit(e) {
e.preventDefault();
if (passwordRef.current.value !== passwordConfirmRef.current.value) {
return setError("Passwords do not match");
}
const promises = [];
setLoading(true);
setError("");
if (emailRef.current.value !== currentUser.email) {
promises.push(updateEmail(emailRef.current.value));
}
if (passwordConfirmRef.current.value) {
promises.push(updatePassword(passwordRef.current.value));
}
Promise.all(promises)
.then(() => {
navigate("/");
})
.catch(() => {
setError("Failed to update account");
})
.finally(() => {
setLoading(false);
});
}
return (
<>
<Card>
<Card.Body>
<h2 className="text-center mb-4">Update Profile</h2>
{error && <Alert variant="danger">{error}</Alert>}
<Form onSubmit={handleSubmit}>
<Form.Group id="email">
<Form.Label>Email</Form.Label>
<Form.Control
type="email"
ref={emailRef}
required
defaultValue={currentUser.email}
/>
</Form.Group>
<Form.Group id="password" data-testid="pword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
ref={passwordRef}
placeholder="Leave blank to keep the same"
/>
</Form.Group>
<Form.Group id="password-confirm">
<Form.Label>Password Confirmation</Form.Label>
<Form.Control
type="password"
ref={passwordConfirmRef}
placeholder="Leave blank to keep the same"
/>
</Form.Group>
<div className="w-100 mt-2">
<Button disabled={loading} className="w-100" type="submit">
Update
</Button>
</div>
</Form>
</Card.Body>
</Card>
<div className="w-100 text-center mt-2">
<Link to="/">Cancel</Link>
</div>
</>
);
}
错误指向第 30 行,在此处以星号表示:
if (emailRef.current.value !== currentUser.email) {
promises.push(updateEmail(emailRef.current.value));
}
if (passwordConfirmRef.current.value) {
**promises.push(updatePassword(passwordRef.current.value));**
}
谁能看出我错在哪里?谢谢!
updateEmail
和 updatePassword
函数可以从 Firebase Auth SDK 导入,而不是 Auth 实例。尝试将您的导入语句更新为:
import { updateEmail, updatePassword } from "firebase/auth"
我有一个带有更新配置文件页面的简单 React 应用程序,使用 firebase 进行身份验证,当我尝试更改密码时,我得到了这个 error:TypeError:updatePassword 不是一个函数。
这是我的UpdateProfile.js
import React, { useRef, useState } from "react";
import { Form, Button, Card, Alert } from "react-bootstrap";
import { getAuth } from "firebase/auth";
import { Link, useNavigate } from "react-router-dom";
export default function UpdateProfile() {
const emailRef = useRef();
const passwordRef = useRef();
const passwordConfirmRef = useRef();
const { currentUser, updateEmail, updatePassword } = getAuth();
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const navigate = useNavigate();
function handleSubmit(e) {
e.preventDefault();
if (passwordRef.current.value !== passwordConfirmRef.current.value) {
return setError("Passwords do not match");
}
const promises = [];
setLoading(true);
setError("");
if (emailRef.current.value !== currentUser.email) {
promises.push(updateEmail(emailRef.current.value));
}
if (passwordConfirmRef.current.value) {
promises.push(updatePassword(passwordRef.current.value));
}
Promise.all(promises)
.then(() => {
navigate("/");
})
.catch(() => {
setError("Failed to update account");
})
.finally(() => {
setLoading(false);
});
}
return (
<>
<Card>
<Card.Body>
<h2 className="text-center mb-4">Update Profile</h2>
{error && <Alert variant="danger">{error}</Alert>}
<Form onSubmit={handleSubmit}>
<Form.Group id="email">
<Form.Label>Email</Form.Label>
<Form.Control
type="email"
ref={emailRef}
required
defaultValue={currentUser.email}
/>
</Form.Group>
<Form.Group id="password" data-testid="pword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
ref={passwordRef}
placeholder="Leave blank to keep the same"
/>
</Form.Group>
<Form.Group id="password-confirm">
<Form.Label>Password Confirmation</Form.Label>
<Form.Control
type="password"
ref={passwordConfirmRef}
placeholder="Leave blank to keep the same"
/>
</Form.Group>
<div className="w-100 mt-2">
<Button disabled={loading} className="w-100" type="submit">
Update
</Button>
</div>
</Form>
</Card.Body>
</Card>
<div className="w-100 text-center mt-2">
<Link to="/">Cancel</Link>
</div>
</>
);
}
错误指向第 30 行,在此处以星号表示:
if (emailRef.current.value !== currentUser.email) {
promises.push(updateEmail(emailRef.current.value));
}
if (passwordConfirmRef.current.value) {
**promises.push(updatePassword(passwordRef.current.value));**
}
谁能看出我错在哪里?谢谢!
updateEmail
和 updatePassword
函数可以从 Firebase Auth SDK 导入,而不是 Auth 实例。尝试将您的导入语句更新为:
import { updateEmail, updatePassword } from "firebase/auth"