【问题标题】:Trying to create firebase user in MST(mobx state tree)action尝试在 MST(mobx 状态树)操作中创建 firebase 用户
【发布时间】:2019-08-16 10:59:22
【问题描述】:

我正在尝试使用 MST 操作在 firebase 中创建新用户。

我的代码如下所示:

.actions((self => ({
    createUserWithEmailPassword:
        flow(function*(password: string) {
            console.log('creating user');
            yield firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
            console.log('set Persistence');
            const user = yield firebase.auth().createUserWithEmailAndPassword(self.email, password);
            console.log('CREATED USER', user);
            self.uid = user.uid;
        })
}));

它确实创建了一个用户,但它不会在createUserWithEmailAndPassword 调用之前进行。 (即它永远不会控制台“创建用户”)

我在用户上也有 onPatch 控制台,但它也不会显示用户更新。

我厌倦了安慰虚假的 api 调用

let res = yield fetch("https://randomapi.com/api/6de6abfedb24f889e0b5f675edc50deb?fmt=raw&sole")

这很好用。

createUserWithEmailAndPassword 似乎有问题,但我想不通。

【问题讨论】:

  • 首先将flow(function*(password: string) { }) 的内部封装到try ... catch 中,以确保它不会引发错误。

标签: firebase react-native mobx mobx-state-tree


【解决方案1】:

您的代码应该可以工作,但您也可以尝试一下

createUserWithEmailPassword(password: string) {   
  flow(function*() {
            console.log('creating user');
            yield firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
            console.log('set Persistence');
            const user = yield firebase.auth().createUserWithEmailAndPassword(self.email, password);
            console.log('CREATED USER', user);
            self.uid = user.uid;
        })() // <--- check this
}

Flow 会返回一个你需要调用的函数

或者像这样

createUserWithEmailPassword(password: string) {
        const run = flow(function*() {
            console.log('creating user');
            yield firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
            console.log('set Persistence');
            const user = yield firebase.auth().createUserWithEmailAndPassword(self.email, password);
            console.log('CREATED USER', user);
            self.uid = user.uid;
        })

        run()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2020-05-22
    • 2019-02-12
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2019-10-18
    相关资源
    最近更新 更多