2017-09-22 23 views
1

我第一次使用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'; 

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; 

我開始在我的AVD的應用程序,我點擊初始化DB (console.log(realm) is empty in the initDB() function),然後當我點擊加載結果,應用程序崩潰,因爲this.realm是不確定的。

我在做什麼錯?

回答

2

訪問的境界,你需要調用Realm.Open().then() ... 改變你的代碼是這樣

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'}, 
    }, 
}; 
相關問題