Firestore文档添加在回调中不起作用

我正在一个项目上,我必须将一个新文档上载到Firestore,这看起来很简单,但是无论何时我尝试使用.doc.set().add()上载任何文档,请求都不会似乎进入了Firebase,但在我的Firestore数据库中什么都没有显示。但是,无论何时我尝试在回调之外上载某些内容,无论它是使用useEffect进行页面加载还是使用任何类型的匿名函数存储在onClick中,都可以正常运行。我正在使用react-redux-firebasereact-reduxredux-firestore。我还没有弄清楚我在做什么错,因此,如果您能指出这一点,那就太好了。

这是保存表格以输入我要上传的信息的组件:

    const New = ({ createPost }) => {
    const [form,setform] = useState({
        title: '',content: ''
    });

    const handleInput = (field,e,data) => {
        switch (field) {
            case 'title':
                setform({ ...form,title: e.target.value });
                break;
            case 'content':
                setform({ ...form,content: data });
                break;
            default:
                return null;
        }
    }

    return (
        <main classname='new'>
            <h1>New Post</h1>
            <form id='new-post-form' onSubmit={() => createPost(form)}>

                <fieldset classname='new-post-field'>
                    <label htmlFor='title'>Title</label>
                    <input onChange={e => handleInput('title',e)} type='text' name='title' id='new-post-title' placeholder='Title'/>
                </fieldset>

                <fieldset classname='new-post-field'>
                    <label htmlFor='content'>Post Content</label>
                    <CKEditor 
                    editor={ ClassicEditor } 
                    onChange={ (e,editor) => {
                        const data = editor.getData();
                        handleInput('content',data)
                    }}
                    />
                </fieldset>

                <fieldset classname='new-post-field'>
                    <input classname='new-form-button' type='submit'/>
                </fieldset>

            </form>
        </main>
    )
}

const mapDispatchToProps = dispatch => ({
    createPost: post => dispatch(createPost(post))
});

export default connect(null,mapDispatchToProps)(New)

Redux动作,负责处理实际发送到Firebase的动作:

export const createPost = post => (
    (dispatch,getState,{ getFirebase,getFirestore }) => {
        const firestore =  getFirestore();
        const slug = post.title.toString().toLowerCase()
            .replace(/\s+/g,'-')
            .replace(/[^\w-]+/g,'')
            .replace(/--+/g,'-')
            .replace(/^-+/,'')
            .replace(/-+$/,'')
            .substring(0,75);
        firestore.collection('posts').doc(slug).set({
            ...post,slug,timeCreated: firestore.FieldValue.serverTimestamp()
        }).then(() => {
            dispatch({ type: 'CREATE_POST',post });
        }).catch(err => {
            dispatch({ type: 'CREATE_POST_ERROR',err })
        });
    }
);
iCMS 回答:Firestore文档添加在回调中不起作用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1788556.html

大家都在问