领域:用户必须属于'对象',得到(未定义)

时间:2017-12-31 10:27:45

标签: react-native realm

每次擦除/重新启动模拟时,都会收到上述错误消息。有没有办法插入一个虚拟用户对象,允许我解析' const'并获得实际登录?

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>
  )
  }
}

1 个答案:

答案 0 :(得分:1)

最后离开了厄运之谷。在这里我的方法:

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 />
      );
    }
  }
}