【问题标题】:How prevent Realm.js from creatig new users if they don't exist?如果 Realm.js 不存在,如何防止他们创建新用户?
【发布时间】:2018-11-02 14:51:37
【问题描述】:

我开始使用 Realm.js 作为我的 Android 应用程序的数据库,但我无法理解为什么如果我使用无效凭据登录它不会给我一个错误,而是会创建一个新用户。

我想避免这种情况发生并给用户一个错误?我是否应该在调用登录函数之前手动检查用户是否存在(之后总是返回true)?

try {
      // Reset any previous errors that might have happened
      this.setState({ error: undefined });
      // Attempt to authenticate towards the server
      const credentials = Realm.Sync.Credentials.usernamePassword(nickname, '123'); // 123 password is hard coded,this is only for testing
      const user = await Realm.Sync.User.login(SERVER_URL, credentials);

      // Hide the modal
      this.setState({ isModalVisible: false });
      this.onAuthenticated(user);
} catch (error) {
      this.setState({ isModalVisible: true, error });
}

【问题讨论】:

    标签: react-native realm realm-mobile-platform realm-js


    【解决方案1】:

    只需在这一行添加 false,用户不是在登录时创建的,而是在凭据构建时创建的。

      const credentials = Realm.Sync.Credentials.usernamePassword(nickname, '123', false); // 123 password is hard coded,this is only for testing
    

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2013-08-05
      相关资源
      最近更新 更多