react-nativemongodb-stitch

Connect to Stitch with React Native


I try to initialize a Stitch default app client, but I always get an error when initializing the client.

Here is my code :

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;

The app Id exists and I can connect it on MongoDB site. Where Am I going wrong ?

Thanks ++ Eric


Solution

  • There is a bug in the test after Stitch.initializeDefaultAppClient.

    It must be

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

    instead of

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

    which is always false because client.auth is made after the test.