Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 为什么react native中未定义领域对象_React Native_Realm - Fatal编程技术网

React native 为什么react native中未定义领域对象

React native 为什么react native中未定义领域对象,react-native,realm,React Native,Realm,我第一次使用Realm,我只是尝试初始化一个数据库并向其写入一条记录。这是我的密码 import React, { Component } from 'react'; import {StackNavigator} from 'react-navigation'; import Realm from 'realm'; import { AppRegistry, StyleSheet, Text, View, Linking } from 'react-native';

我第一次使用Realm,我只是尝试初始化一个数据库并向其写入一条记录。这是我的密码

import React, { Component } from 'react';
import {StackNavigator} from 'react-navigation';
import Realm from 'realm';


import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Linking
} from 'react-native';

class Home extends Component {
  static navigationOptions = {
    title: 'Login',
    headerStyle: {
        backgroundColor:'#000000'
    },
    headerTitleStyle: {
        color:'#fff'
    }
  };
  initDB()
  {
    const realm = new Realm({schema: [CompatibilityProduct]});
    this.realm = realm;
    console.log(realm);
  };
  loadResults()
  {
    const realm = this.realm;
    console.log(realm);
        // Write
    realm.write(() => {
        realm.create('CompatibilityProduct', {
            compatibilityId: 18,
            productId: 17,
        });
    });


  };
    constructor(props)
    {
        super(props);
    }
  render() {
    const {navigate} = this.props.navigation;
    return (
      <View>
        <Text onPress={this.initDB}>
             Init DB
        </Text>
        <Text onPress={this.loadResults}>
             Load Results
        </Text>
      </View>
    );
  }
}

const myscreens = StackNavigator({
  Home: {screen: Home}
});

class CompatibilityProduct {}
CompatibilityProduct.schema = {
    name: 'CompatibilityProduct',
    properties: {
        compatibilityId: {type: 'int'},
        productId: {type: 'int'},
    },
};


export default myscreens;
import React,{Component}来自'React';
从“react navigation”导入{StackNavigator};
从“领域”导入领域;
进口{
评估学,
样式表,
文本,
看法
连接
}从“反应本机”;
类Home扩展组件{
静态导航选项={
标题:“登录”,
头型:{
背景颜色:'#000000'
},
头饰样式:{
颜色:'#fff'
}
};
initDB()
{
const realm=新领域({schema:[CompatibilityProduct]});
this.realm=领域;
log(领域);
};
加载结果()
{
const realm=this.realm;
log(领域);
//写
realm.write(()=>{
realm.create('CompatibilityProduct'{
相容性ID:18,
产品编号:17,
});
});
};
建造师(道具)
{
超级(道具);
}
render(){
const{navigate}=this.props.navigation;
返回(
初始化数据库
加载结果
);
}
}
const myscreens=StackNavigator({
主页:{屏幕:主页}
});
类兼容性产品{}
CompatibilityProduct.schema={
名称:'CompatibilityProduct',
特性:{
兼容性ID:{type:'int'},
productId:{type:'int'},
},
};
导出默认屏幕;
我在AVD中启动应用程序,单击Init DB
(console.log(realm)在initDB()函数中为空)
,然后当我单击Load Results时,应用程序崩溃,因为
此.realm
未定义


我做错了什么?

要访问域,需要调用
realm.Open()。然后()…
将代码更改为如下所示

 class Home extends React.Component {
  constructor(props) {
   super(props);
   this.state = { realm: null }; // initial the state with null
  }
  initDB()
  {
   Realm.open({ // open connection
     schema: [CompatibilityProduct]
   }).then(realm => { // here is realm
    console.log('realm1', realm);
    this.setState({ realm });  // set it to state
   });
  };
 loadResults()
  {
const {realm } = this.state; // access it from state
console.log('realm2', realm);
    // Write
realm.write(() => { // write to it
    realm.create('CompatibilityProduct', {
        compatibilityId: 18,
        productId: 17,
    });
  });
  this.setState({ realm }); // set it to state to see updates
};

render() {
// read data from it
const info = this.state.realm ? 'Number of CompatibilityProduct in this Realm: ' + this.state.realm.objects('CompatibilityProduct').length : 'Loading...';

return (
  <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text onPress={this.initDB.bind(this)}> // bind to access this
         Init DB
    </Text>
     <Text onPress={this.loadResults.bind(this)}> // bind to access this
         Load Results {info}
      </Text>
    </View>
  );
 }
}


const CompatibilityProduct = { // schema is object not class
   name: 'CompatibilityProduct',
    properties: {
    compatibilityId: {type: 'int'},
    productId: {type: 'int'},
  },
 };
class Home扩展了React.Component{
建造师(道具){
超级(道具);
this.state={realm:null};//用null初始化状态
}
initDB()
{
Realm.open({//open connection
架构:[兼容性产品]
}).然后(realm=>{//这里是realm
log('realm1',realm);
this.setState({realm});//将其设置为state
});
};
加载结果()
{
const{realm}=this.state;//从state访问它
log('realm2',realm);
//写
realm.write(()=>{//写入它
realm.create('CompatibilityProduct'{
相容性ID:18,
产品编号:17,
});
});
this.setState({realm});//将其设置为state以查看更新
};
render(){
//从中读取数据
const info=this.state.realm?'此领域中CompatibilityProduct的数量:'+this.state.realm.objects('CompatibilityProduct')。长度:'Loading…';
返回(
//绑定以访问此文件
初始化数据库
//绑定以访问此文件
加载结果{info}
);
}
}
常量CompatibilityProduct={//架构不是对象类
名称:'CompatibilityProduct',
特性:{
兼容性ID:{type:'int'},
productId:{type:'int'},
},
};