React native 领域:用户的类型必须为';对象';,got(未定义)

React native 领域:用户的类型必须为';对象';,got(未定义),react-native,realm,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: '

每次我擦除/重新启动模拟时,都会收到上述错误消息。有没有一种方法可以插入一个虚拟用户对象,允许我通过“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>
  )
  }
}
从“React”导入React;
从“react native”导入{样式表、AppRegistry、TextInput、文本、视图、警报、};
从“领域”导入领域;
logBookSchema={schema:[{
名称:‘行车日志’,
特性:{
logNum:'int',
},
}]
}
类日志扩展了Realm.Object{}
const realm=新领域({sync:{user:realm.sync.user.current,url:'realm://localhost:9080/~/logbook',错误:err=>alert(err)},logBookSchema});
导出默认类App扩展React.Component{
render(){
Realm.Sync.User.login('http://localhost:9080', 'test@user2.com,然后(用户=>{
Realm.open({logBookSchema,sync:{user:user,url:'realm://localhost:9080/~/logbook',错误:err=>alert(err)});
});
返回(
登录
)
}
}

终于离开了厄运谷。以下是我的做法:

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 />
      );
    }
  }
}
导出默认类LoginScreen扩展React.Component{
建造师(道具){
超级(道具);
this.state={realm:null};
}
组件willmount(){
Realm.Sync.User.login('http://192.168.2.105:9080', 'test@user2.com","测试")
。然后(用户=>{
Realm.open({schema:[LogBook],
同步:{用户:用户,url:'realm://192.168.2.105:9080/~/logbook',错误:err=>alert(err)}
})
.然后(领域=>{
this.setState({realm});
});
});
}
render(){
如果(!this.state.realm){return(
加载
)} 
否则{
返回(
);
}
}
}