【问题标题】:Connect to Stitch with React Native使用 React Native 连接到 Stitch
【发布时间】:2019-12-07 02:09:25
【问题描述】:

我尝试初始化 Stitch 默认应用客户端,但在初始化客户端时总是出错。

这是我的代码:

import React from 'react';
import {View, Text, SafeAreaView} from 'react-native';
import {Stitch, AnonymousCredential} from 'mongodb-stitch-react-native-sdk';

class App extends React.Component {
  render() {
    return (
      <SafeAreaView>
        <View>
          <Text>Stitch Test</Text>
        </View>
      </SafeAreaView>
    );
  }

  async componentDidMount() {
    console.log('=> App/componentDidMount');
    await this._loadClient();
  }

  _loadClient() {
    console.log('=> App/_loadClient');

    Stitch.initializeDefaultAppClient('tuto - nbmzq').then(client => {
      if (client.auth.isLoggedIn) {
        client.auth
          .loginWithCredential(new AnonymousCredential())
          .then(user => {
            console.log('Successfully logged in as user ${user.id}');
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        console.log(client);
        console.log('Error initialize Client');     // Always fall here
      }
    });
  }
}

export default App;

应用 ID 存在,我可以在 MongoDB 站点上连接它。 我哪里错了?

谢谢 ++ 埃里克

【问题讨论】:

  • 这似乎是一个特定于库的问题,你应该在那里打开一个新问题 git repo stitch-js-sdk

标签: react-native mongodb-stitch


【解决方案1】:

Stitch.initializeDefaultAppClient之后的测试有bug。

应该是

if (client.auth.hasDeviceId) { ... }

而不是

if (client.auth.isLoggedIn) { ... }

这总是错误的,因为client.auth 是在测试之后生成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 2019-02-14
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多