2017-11-29 2 views
0

React 네이티브 프로젝트에 영역 데이터베이스가 있습니다. 다른 페이지에서 언어 값을 업데이트하고 싶습니다. 처음에는 값을 쓸 수 있었지만 업데이트가되지 않았습니다. 아래는 제 코드입니다.React Native Realm Database의 값 업데이트

프로필 스키마 (영역 데이터베이스 스키마)

'엄격한 사용'; 내가 값을 업데이트 할 필요가

import Realm from 'realm'; 

class Profile extends Realm.Object {} 
Profile.schema = { 
    name: 'Profile', 
    properties: { 
     Onetime: 'string', 
     Name: 'string', 
     Email: 'string', 
     Phone: 'string', 
     Language:'string', 
     Url:'string', 
    }, 
}; 



export default new Realm({schema: [Profile]}); 

로드 초기 데이터

let objects = realm.objects('Profile'); 

    var name,url,phone,onetime; 


    if (firebase.auth().currentUser.displayName == null) { 
     onetime= 'true'; 
     name= 'Name'; 
     url = 'https://media2.giphy.com/media/sbLpwwHlgls8E/giphy.gif'; 
     phone = '0000000000'; 
    } 
    else { 
     onetime= 'true'; 
     name=firebase.auth().currentUser.displayName; 
     url=firebase.auth().currentUser.photoURL; 
     phone = '0000000000'; 
    } 

    if (objects.length == 0) { 

    realm.write(() => { 
    realm.create('Profile', { Onetime: onetime, Name: name, Email: firebase.auth().currentUser.email, Phone:phone, Language: 'e', Url: url, }); 
    }); 
    } 

    else { 
     realm.write(() => { 
     realm.delete(objects); 
     realm.create('Profile', { Onetime: onetime, Name: name, Email: firebase.auth().currentUser.email, Phone:phone, Language: 'e', Url: url, }); 
    }); 


    } 

활동

import React from 'react'; 
import { 
    ScrollView, 
    View, 
    StyleSheet 
} from 'react-native'; 
import { 
    RkText, 
    RkTextInput, 
    RkAvoidKeyboard, 
    RkTheme, 
    RkStyleSheet 
} from 'react-native-ui-kitten'; 
import {SocialSetting} from '../../components'; 
import {FontAwesome} from '../../assets/icons'; 
import {GradientButton} from '../../components'; 
import Avatar from 'react-native-interactive-avatar'; 
import ImagePicker from 'react-native-image-crop-picker'; 
import realm from '../../realm'; 
import firebase from 'firebase'; 
import {RkSwitch} from '../../components/switch/index'; 
import RadioForm, {RadioButton, RadioButtonInput, RadioButtonLabel} from 'react-native-simple-radio-button'; 

var radio_props = [ 
    {label: 'English  ', value: 'e' }, 
    {label: 'Malayalam', value: 'm'} 

]; 

var lange = ''; 

var objects = realm.objects('Profile'); 

export class ProfileSettings extends React.Component { 
    static navigationOptions = { 
    title: 'Profile Settings'.toUpperCase() 
    }; 

    constructor(props) { 
    super(props); 




    this.state = { 
     name: objects[0].Name, 
     email: objects[0].Email, 
     phone: objects[0].Phone, 
     language:objects[0].Language, 
     url:objects[0].Url, 
     lang:'' 

    } 

    } 




    pickimage(){ 

     ImagePicker.openPicker({ 
     width: 300, 
     height: 400, 
     cropping: true 
     }).then(image => { 
     console.log("imagelink- "+image); 
     }); 
    } 

    handleLogOut() { 
     firebase.auth().signOut(); 
    } 

    handleSave() { 

      alert("Language is: "+lange); 


    } 

    updateuser(){ 

     var user = firebase.auth().currentUser; 

    user.updateProfile({ 
    displayName: this.state.name, 
    email: this.state.email 
}).then(function() { 
    alert("Update SuccessFull"); 
}).catch(function(error) { 
    // An error happened. 
    alert("Update Failed"); 
}); 


    } 



    render() { 

    if (this.state.language == 'e') { 

     var val = 0; 

     } 

     else { 
     var val = 1; 
     } 




    return (
     <ScrollView style={styles.root}> 
     <RkAvoidKeyboard> 
      <View style={styles.header}> 
      <Avatar 
       uri={this.state.url} 
       size={'default'} 
      /> 
      </View> 
      <View style={styles.section}> 
      <View style={[styles.row, styles.heading]}> 
       <RkText rkType='header6 primary'>INFO</RkText> 
      </View> 
      <View style={styles.row}> 
       <RkTextInput label='Name' 
          value={this.state.name} 
          rkType='right clear' 
          onChangeText={(text) => this.setState({name: text})}/> 
      </View> 

      <View style={styles.row}> 
       <RkTextInput label='Email' 
          value={this.state.email} 
          onChangeText={(text) => this.setState({email: text})} 
          rkType='right clear'/> 
      </View> 



      </View> 



      <View style={styles.section}> 
      <View style={[styles.row, styles.heading]}> 
       <RkText rkType='primary header6'>CHOOSE YOUR LANGUAGE</RkText> 
      </View> 

      <View> 
      <View style={styles.radio}> 
      <RadioForm 
      radio_props={radio_props} 
      initial={val} 
      onPress={(value) => { 
       { 
       this.setState({lang:value}) 
       this.setState({language: this.state.lang}) 
       lange = value; 


      }}} 
      /> 

      </View> 
     </View> 
      </View> 
      <GradientButton rkType='large' style={styles.button} text='SAVE' onPress={this.handleSave} /> 
      <GradientButton rkType='large' style={styles.button} text='LOGOUT' onPress={this.handleLogOut}/> 
     </RkAvoidKeyboard> 
     </ScrollView> 
    ) 
    } 
} 

let styles = RkStyleSheet.create(theme => ({ 
    root: { 
    backgroundColor: theme.colors.screen.base 
    }, 
    header: { 
    backgroundColor: theme.colors.screen.neutral, 
    paddingVertical: 25, 
    justifyContent: 'center', 
    alignItems: 'center' 
    }, 
    section: { 
    marginVertical: 25 
    }, 
    radio: { 
    flexDirection:'row', 
    margin:20 
    }, 
    heading: { 
    paddingBottom: 12.5 
    }, 
    row: { 
    flexDirection: 'row', 
    paddingHorizontal: 17.5, 
    marginTop:15, 
    borderBottomWidth: StyleSheet.hairlineWidth, 
    borderColor: theme.colors.border.base, 
    alignItems: 'center', 
    justifyContent: 'space-between', 
    flex:1 
    }, 
    button: { 
    marginHorizontal: 16, 
    marginBottom: 32 
    } 
})); 

I합니다 (handleSave에서 언어의 값을 업데이트 할)

도움을 주시면 감사하겠습니다.

미리 감사드립니다.

답변

1

문제가 해결되었습니다. 해결책은 다음과 같습니다

handleSave() { 

      //alert("Language is: "+lange); 
      let updt = realm.objects('Profile'); 
      realm.write(() => { 
      updt[0].Language = lange; 
}); 

     alert("Language is: "+updt[0].Language); 


    } 

스키마 개체를 만들고 특정 필드 값을 업데이트하기 만하면됩니다.

+0

나를 위해 일했습니다 .... 감사합니다 – user5800586

+0

@ user5800586 그레이트 – vishnumm93

관련 문제