简体   繁体   English

无法通过联系表将数据提交至 firebase

[英]Not able to submit the data to firebase from contact form

import React, { useState } from 'react'
        import styled from 'styled-components'
        import Title from '../Components/Title'
        import { InnerLayout, MainLayout } from '../Styles/Layout'
        import Button from '../Components/Button'
        import { db } from '../firebase';
        
        function Contact() {
            const [name, setName] = useState("");
            const [email, setEmail] = useState("");
            const [message, setMessage] = useState("");
            const [subject, setSubject] = useState("");
        
            const handleSubmit = (e) => {
                e.preventDefault();
        
                db.collection('mail').add({
                    name: name,
                    email: email,
                    subject: subject,
                    message: message,
                })
                    .then(() => {
                        alert("Thank you for contacting. Your message has been sent successfully.");
                    })
                    .catch((err) => {
                        alert(err.message);
                    });
        
                setName('')
                setEmail('')
                setSubject('')
                setMessage('')
            };
        
            return (
                <MainLayout>
                    <Title title={'Contact'} span={'Contact'} />
                    <ContactMain>
                        <InnerLayout className='contact-section'>
                            <div className="left-content">
                                <div className="contact-title">
                                    <h4>Get In Touch</h4>
                                </div>
                                <form className="form" onSubmit={handleSubmit}>
                                    <div className="form-field">
                                        <label htmlFor="name">Enter Your Name</label>
                                        <input type="text" id="name" value={name} onChange={(e) => setName(e.target.value)} />
                                    </div>
                                    <div className="form-field">
                                        <label htmlFor="email">Enter Your Email</label>
                                        <input type="email" id="email" value={email} onChange={(e) => setEmail(e.target.value)} />
                                    </div>
                                    <div className="form-field">
                                        <label htmlFor="subject">Enter Your Subject</label>
                                        <input type="text" id="subject" value={subject} onChange={(e) => setSubject(e.target.value)} />
                                    </div>
                                    <div className="form-field">
                                        <label htmlFor="text-area">Enter Your Message</label>
                                        <textarea name="textarea" id="textarea" cols="30" rows="10" value={message} onChange={(e) => setMessage(e.target.value)}></textarea>
                                    </div>
                                    <div className="form-field f-button">
                                        <Button title="Send Email" />
                                    </div>
                                </form>
                            </div>
                        </InnerLayout>
                    </ContactMain>
                </MainLayout>
        
            )
    }

I don't know why but I am not able to send the details to my firebase database.我不知道为什么,但我无法将详细信息发送到我的 firebase 数据库。 I am not able to find the issue in this code.我无法在此代码中找到问题。 I have copied the firebase database key and all in the firebase.js and then imported it in this contact.js and I then made the necessary changes in this.我复制了 firebase 数据库密钥和 firebase.js 中的所有内容,然后将其导入到此 contact.js 中,然后我对此进行了必要的更改。 Still, I am not able to figure out the issue.尽管如此,我还是无法弄清楚这个问题。

I would reset the form once the promise returned by add() is resolved.一旦add()返回的承诺得到解决,我将重置表单。

const handleSubmit = (e) => {
  e.preventDefault();
    
  db.collection('mail').add({
    name: name,
    email: email,
    subject: subject,
    message: message,
  }).then(() => {
    // Reset those states here
    setName('')
    setEmail('')
    setSubject('')
    setMessage('')
    alert("Thank you for contacting. Your message has been sent successfully.");
  }).catch((err) => {
    alert(err.message);
  });
};

I guess your states are being reset to "" before the document is added as those setState methods would have ran before the doc was added.我猜您的状态在添加文档之前被重置为"" ,因为这些setState方法会在添加文档之前运行。

Potentially because your button html within its the button component type is not set to 'submit' - I had the same issue I think which ended up being super simple.可能是因为按钮组件类型中的按钮 html 未设置为“提交”-我遇到了同样的问题,我认为最终变得非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM