react-native - Realm :用户必须是 'object' 类型,得到(未定义)

标签 react-native realm

每次我删除/重新启动我的模拟时,我都会收到上述错误消息。有没有办法插入一个虚拟用户对象,使我能够通过“常量”进行解析并获得实际登录?

import React from 'react';
import { StyleSheet, AppRegistry, TextInput, Text, View, Alert, } from 'react-native';
import Realm from 'realm';

logBookSchema = {schema: [{
      name: 'LogBook',
      properties: {
        logNum: 'int',
      },
    }]
}

class LogBook extends Realm.Object {}

const realm = new Realm({sync: {user: Realm.Sync.User.current, url: 'realm://localhost:9080/~/logbook',error: err => alert(err)},logBookSchema});

export default class App extends React.Component {
  render() {
    Realm.Sync.User.login('http://localhost:9080', 'test@user2.com', 'test').then (user => {
        Realm.open({logBookSchema, sync: {user: user, url: 'realm://localhost:9080/~/logbook',error: err => alert(err)}});
      });

  return (
     <View><Text></Text><Text>Logged in</Text></View>
  )
  }
}

最佳答案

终于离开了厄运之谷。这是我的方法:

export default class LoginScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = { realm: null };
  }

  componentWillMount() {
    Realm.Sync.User.login('http://192.168.2.105:9080', 'test@user2.com', 'test')
    .then (user => {
      Realm.open({schema: [LogBook],
        sync: {user: user, url: 'realm://192.168.2.105:9080/~/logbook',error: err => alert(err)}
      })
      .then(realm => {
      this.setState({ realm });
      });
    });
  }

  render() {
    if (!this.state.realm) {return (
      <View><Text></Text><Text>Loading</Text></View>
    )} 
    else {
      return (
        <MyApp />
      );
    }
  }
}

关于react-native - Realm :用户必须是 'object' 类型,得到(未定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041699/

相关文章:

ios - 在 xcode IOS 中锁定 React Native 版本

swift - Realm Swift 结果在索引处获取对象

swift - 使用 Swift ObjectMapper 库访问映射中的 JSONString

javascript - 帕霍 MQTT : Possible importing error?

android - react native map : Markers image doesn't show using Custom Marker in react-native-maps

react-native - 使用 react-navigation 单击每个选项卡时如何显示初始路线

ios - 我如何保存 NSDecimalNumber 以外的货币?

ionic-framework - 在 ionic 应用程序中使用 Realm ?

ios - cellForRowAt indexPath 中 Realm 对象的 UITableView 性能

android - 当 i18n.locale 更改时更改原生 android 上的启动图像