我正在尝试向Firebase节点添加一个值,而我当前的代码正在覆盖已存在的内容。
我的数据库设置是这样的(我试图将UID添加到"参与者"):
"rooms" : {
"-Ki6fAHv6LS5pyx34nUr" : {
"messages" : {
"-Ki70oJOAsto1uIxJsLY" : {
"senderId" : "tzfHgGKWLEPzPU9GvkO4XE1QKy53",
"senderName" : "Timothy",
"text" : "Test"
}
},
"participants" : {
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : true
},
"roomName" : "Room One"
}
这是我的代码,当用户搜索其他用户时运行,然后点击结果:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Add selected user's UID to current room's participants
let currentRoomParticipantsRef: FIRDatabaseReference = FIRDatabase.database().reference().child("rooms").child(currentRoomID).child("participants")
let newParticipantItem = [self.returnedUsersUID : true]
currentRoomParticipantsRef.setValue(newParticipantItem)
let alertController = UIAlertController(title: "Added!", message: "Your friend has been added to the room", preferredStyle: UIAlertControllerStyle.actionSheet)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: {(alert :UIAlertAction!) in
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
这样做会覆盖"参与者"中已列出的UID [s]。有了新的价值。如何将新UID添加到列表中,而不是覆盖?
答案 0 :(得分:2)
而不是import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native';
export default class Preload extends Component {
render() {
return (
<View style={styles.mainBox}>
<Image source={require('../app_images/preload/bg.jpg')} style={styles.bgImage} />
<View><Image source={require('../app_images/preload/logo.png')} style={styles.logoImage} /></View>
</View>
);
}
}
const styles = StyleSheet.create({
mainBox:{
flex: 1,
alignItems:'center',
justifyContent:'center',
width: null,
height: null
},bgImage: {
position:'absolute',
top: 0,
left: 0,
alignSelf: 'stretch'
},logoImage: {
width: 100,
height: 150
}
});
AppRegistry.registerComponent('Preload', () => Preload);
(确实用新词典替换整个节点),尝试使用updateChildValues
,它将更新值(或创建新的值),而不会覆盖已存在的键。