如果新用户不存在,如何防止 Realm.js 创建新用户?

How prevent Realm.js from creatig new users if they don't exist?

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

我想避免这种情况发生并给用户一个错误?我是否应该在调用登录函数之前手动检查用户是否存在(之前总是 return 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 });
}

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

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