首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >JS函数使用Firebase时出错并作出反应

JS函数使用Firebase时出错并作出反应
EN

Stack Overflow用户
提问于 2022-03-11 08:42:47
回答 1查看 27关注 0票数 0

谢谢你的耐心。我正在我的站点上实现Firebase,但是当我在FormUp.js中调用注册函数(在AuthContext.js中声明)时,它没有引用函数定义。这将导致在FormUp.js中调用函数,而不是调用在AuthContext.js中定义的自己的函数,而是落入catch分支(“未能创建帐户”)。我不明白为什么。希望有人能帮我,谢谢!错误:

代码语言:javascript
运行
AI代码解释
复制
TypeError: _firebase__WEBPACK_IMPORTED_MODULE_1__.auth.createUserWithEmailAndPassword is not a function
    at signup (bundle.js:4226:56)
    at handleSubmit (bundle.js:2575:13)
    at HTMLUnknownElement.callCallback (bundle.js:41062:18)
    at Object.invokeGuardedCallbackDev (bundle.js:41111:20)
    at invokeGuardedCallback (bundle.js:41171:35)
    at invokeGuardedCallbackAndCatchFirstError (bundle.js:41186:29)
    at executeDispatch (bundle.js:45421:7)
    at processDispatchQueueItemsInOrder (bundle.js:45453:11)
    at processDispatchQueue (bundle.js:45466:9)
    at dispatchEventsForPlugins (bundle.js:45477:7)

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
    at Navbar (http://localhost:3000/static/js/bundle.js:3361:76)
    at Home

代码:

AuthContext.js

代码语言:javascript
运行
AI代码解释
复制
import React, { useContext, useState, useEffect } from "react";
import { auth } from "../firebase";
import { createUserWithEmailAndPassword } from "firebase/auth";

const AuthContext = React.createContext();

export function useAuth() {
  return useContext(AuthContext);
}

export function AuthProvider({ children }) {
  const [currentUser, setCurrentUser] = useState();
  const [loading, setLoading] = useState(true);

  function signup(email, password) {
    return auth.createUserWithEmailAndPassword(email, password);
  }

  function login(email, password) {
    return auth.signInWithEmailAndPassword(email, password);
  }

  function logout() {
    return auth.signOut();
  }

  function resetPassword(email) {
    return auth.sendPasswordResetEmail(email);
  }

  function updateEmail(email) {
    return currentUser.updateEmail(email);
  }

  function updatePassword(password) {
    return currentUser.updatePassword(password);
  }

  useEffect(() => {
    const unsubscribe = auth.onAuthStateChanged((user) => {
      setCurrentUser(user);
      setLoading(false);
    });

    return unsubscribe;
  }, []);

  const value = {
    currentUser,
    login,
    signup,
    logout,
    resetPassword,
    updateEmail,
    updatePassword,
  };

  return (
    <AuthContext.Provider value={value}>
      {!loading && children}
    </AuthContext.Provider>
  );
}

firebase.js

代码语言:javascript
运行
AI代码解释
复制
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
  //Hidden
};

const app = initializeApp(firebaseConfig);
const auth = getAuth();
export { app, auth };

FormUp.js

代码语言:javascript
运行
AI代码解释
复制
import React, { useRef, useState } from "react";
import { Link } from "react-router-dom";
import {
  FormUser,
  Input,
  Label,
  Subtitle,
  TextWrapper,
  TopLine,
  FormButton,
  Credentials,
  HomePage,
  SignInLink,
  SignInText,
  RedirectSignIn,
  Credential,
} from "./Form.elements";
import { FaAngleLeft } from "react-icons/fa";
import { useAuth } from "../../contexts/AuthContext";
import { Alert } from "bootstrap";

const FormUp = ({
  primary,
  lightBg,
  lightTopLine,
  lightTextDesc,
  buttonLabel,
  description,
  topLine,
}) => {
  const emailRef = useRef();
  const passwordRef = useRef();
  const passwordConfirmRef = useRef();
  const { signup } = useAuth();
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);

  async function handleSubmit(e) {
    e.preventDefault();

    if (passwordRef.current.value !== passwordConfirmRef.current.value) {
      return setError("Password do not match");
    }

    try {
      setError("");
      setLoading(true);
      await signup(emailRef.current.value, passwordRef.current.value);
    } catch {
      setError("Failed to create an account");
    }

    setLoading(false);
  }

  return (
    <>
      <style>
        @import
        url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap');
      </style>
      <FormUser lightBg={lightBg} onSubmit={handleSubmit}>
        <TextWrapper>
          <HomePage href="/">
            <FaAngleLeft />
            <TopLine lightTopLine={lightTopLine}>{topLine}</TopLine>
          </HomePage>
          <Subtitle lightTextDesc={lightTextDesc}>{description}</Subtitle>
          {error && (
            <h5 style={{ color: "red", paddingBottom: "30px" }}>{error}</h5>
          )}
          <Credentials>
            <Credential id="email">
              <Label>Email</Label>
              <Input
                ref={emailRef}
                required
                type="email"
                placeholder="Email..."
              ></Input>
            </Credential>
            <Credential id="password">
              <Label>Password</Label>
              <Input
                ref={passwordRef}
                required
                type="password"
                placeholder="Password..."
              ></Input>
            </Credential>
            <Credential id="password-confirm">
              <Label>Password confirmation</Label>
              <Input
                ref={passwordConfirmRef}
                required
                type="password"
                placeholder="Password confirmation..."
              ></Input>
            </Credential>
          </Credentials>

          <FormButton disabled={loading} type="submit" big primary={primary}>
            {buttonLabel}
          </FormButton>

          <RedirectSignIn>
            <SignInText>Already have an account?</SignInText>
            <SignInLink href="/sign-in">Log in</SignInLink>
          </RedirectSignIn>
        </TextWrapper>
      </FormUser>
    </>
  );
};

export default FormUp;
EN

回答 1

Stack Overflow用户

发布于 2022-03-11 08:59:16

这里的问题是,正如错误消息所暗示的,auth没有一个名为createUserWithEmailAndPassword的属性。您正在搜索的不是auth的一部分。您的代码应该类似于

代码语言:javascript
运行
AI代码解释
复制
function signup(email, password) {
  return createUserWithEmailAndPassword(auth, email, password);
}

而不是

代码语言:javascript
运行
AI代码解释
复制
function signup(email, password) {
  return auth.createUserWithEmailAndPassword(email, password);
}

检查此选项(如果使用的是SDK的版本8,则需要使用不同的方法)

https://firebase.google.com/docs/auth/web/password-auth#create_a_password-based_account

我的建议是在您的项目中添加一个链接,以方便地捕捉这些问题。在这种情况下,由于没有使用createUserWithEmailAndPassword的导入,该问题将立即可见。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71441847

复制
相关文章
iOS键盘防键盘遮挡库 KKInputAvoidKeyBoard 每个 UITextField 都可以自己控制
/* 键盘遮挡后,是否自动调整,防止键盘遮挡 */ var isAvoidKeyBoardEnable: Bool
onety码生
2018/11/21
1.5K0
iOS UITextField 显示银行卡格式
输入框显示银行卡格式,即为每隔4位出现一个空格, 下面使用UITextFieldDelegate,编码实现: 首先引用使用代理
网罗开发
2021/01/29
8340
Android 隐藏显示键盘
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/49105161
Hankkin
2018/09/06
1.9K0
罗技k580键盘蓝牙连接不上_ipad同时连接蓝牙键盘和耳机
拔下键盘的USB接口,长按F11,F11的灯急促闪烁,在电脑/手机的蓝牙找到K580名的设备,进行配对,输入数字,配对成功。
全栈程序员站长
2022/09/27
5.6K1
iOS-UITextField 全面解析iOS中UITextField 使用全面解析UITextField的代理方法通知UITextField 在storyboard 中设置属性
iOS中UITextField 使用全面解析 建议收藏,用到的时候来这里一查就都明白了 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef NS_ENUM(NSInteger, UI
xx_Cc
2018/05/10
7.3K0
将jpeg图片显示在framebuffer上
点击(此处)折叠或打开 /************************************************** * example5.c * Author: T-bagwell * * Compile:gcc -Wall example5.c -o example5 *************************************************/ #include <stdio.h> #inclu
用户3765803
2019/03/05
1.2K0
联想笔记本键盘亮了屏幕不亮怎么办_电脑开机显示器和键盘都不亮
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
全栈程序员站长
2022/11/05
2.5K0
ios13 UITextField 设置leftView不响应不生效的问题
UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 18, _phoneTextField.height)]; UIImageView *phoneImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_phone"]]; phoneImage.frame = CGRectMake(0, 10, 18, 23); [phoneView addS
Lee坚武
2020/01/08
1.8K0
touchesBegan: withEvent: 不执行/完美收起键盘
– (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
全栈程序员站长
2022/09/15
8600
Windows如何优雅显示键盘按键? ​
​最近打算做一个 「昭昭的奇妙技能书」系列(取名借鉴自 「JOJO的奇妙冒险」),以问答的形式开篇,用简短的gif图演示,文章最后放出软件的下载链接,每次只讲软件与问答相关的小功能,争取用最少的字,最简洁的图,传递最有用的技能~
zhaoolee
2020/03/26
1.3K0
1.注册或登录页面设计:UILabel,UIButton,UITextField
学习iOS开发已经有一段时日了,之前一直没有系统的对iOS开发的相关知识进行归纳总结,导致很多知识点云里雾里在脑子里形不成iOS开发的思想,现将自己在学习过程中遇到的一些知识进行总结,希望能对iOS初学者能有一定的帮助。最初学iOS的时候苦于没有大神指点,全靠自己一点点摸索,确实走了很多弯路,不希望还有小伙伴跟我一样走过多的弯路。   由于本人只是从去年11月份才开始玩iOS(附上自己的学习路线,如下图),受限于能力,难免有一些不完善或不恰当的地方,希望大神们多多见谅,勿拍砖,有不足或需要完善的地方也希望
猿人谷
2018/01/17
2.4K0
1.注册或登录页面设计:UILabel,UIButton,UITextField
iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决
  最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待输入的cell,导致我们无法很方便地查看到我们输入的内容,这样的体验是非常不好的。这个问题在之前我们的随笔iOS学习——键盘弹出遮挡输入框问题解决方案中也有讲过对应的解决方案,但是该方案在最近的应用中还有点小问题,我们在这里重新进行处理好。 一 主控制器为UITableViewController或其子类
mukekeheart
2018/04/04
4K0
iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决
iOS UITextField详解
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。
码客说
2019/10/22
1.8K0
Android在ImageView上直接显示网络图片
在原生的ImageView中,没有一个方法是可以直接显示网络的图片的,当我们经常需要显示网络图片时,每次都有一大堆的操作,这会很麻烦,今天就教大家在ImageView上轻松显示网络图片。
夜雨飘零
2020/05/06
6.3K0
iOS UITextField 限制输入
但是如果很多页面的TextField控件需要验证这样写就很麻烦,为了不重复造轮子。写了一个Category。只实现了输入长度限制,输入数字,输入字母,如果不能满足需求可自行扩展。
赵哥窟
2019/01/28
1.2K0
iOS开发——定制UITextField
在iOS中UITextField这个控件作为文本输入控件一定是使用率最高的几个控件之一,而iOS提供的默认的原始TextField的造型肯定在开发时很难满足我们的要求,原因很简单,不够美观,实在太单调。所以今天我们从一些简单的复写UITextField方法开始,来讲一讲如何定制一个属于自己的UITextField。
Originalee
2018/08/30
1.6K0
DNSPod“码”上送CHERRY键盘
每逢农历正月十五,各家各户都要挂起彩灯,燃放焰火,猜灯谜。元宵节,咱来个“猜代码,赢大奖”。 一、参与方式 关注“DNSPod“公众微信帐号,直接发送答案:A(或B或C或D)到DNSPod公众微信平台,即可直接参与活动。 请看谜面: 点击“D活动-参与活动”,立刻“猜代码”。 二、活动时间 2014年2月13日10:30-2月14日18:00 三、活动奖品 马上有豪礼:限量CHERRY机械键盘 马上有限量:2014马年限量台历、限量IPhone手机壳 马上有套餐:DNSPod产品打折券 四、活动及奖品
腾讯云DNSPod团队
2023/05/04
2240
DNSPod“码”上送CHERRY键盘
iOS UITextField 文本输入框
///UILabel 显示的文本只读,无法编辑,可以根据文字个数自动换行; ///UITextField 可编辑本文,但是无法换行,只能在一行显示;当点击键盘上的return时会收到一个事件做一些事情。 ////UITextView 可编辑文本,提供换行功能。
Lee坚武
2020/01/19
3.2K0
IOS UITextField 事件列表
//UITextFieldDelegate import UIKit class ViewController:UIViewController,UITextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let rect = CGRect(x:10, y:80, wid
用户5760343
2019/07/08
1.1K0
点击加载更多

相似问题

使用UIPopover显示键盘

31

uipopover在键盘显示时隐藏

20

UIPopover内部的UITextField

11

如何在UITextField显示时保持活动UIPopover;

10

在UITableView中获取用于显示UIPopover的UITextField框架

13
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文