如何将Firebase中的节点(对象)复制到另一个节点?

时间:2016-09-28 11:14:33

标签: javascript angularjs firebase firebase-realtime-database

我在Firebase中有一个名为Events的节点。它由以下子对象组成:addressdescriptionlongitudelatitude。在用户删除事件节点之前,我想将其复制到同一个数据库中,并将其复制到名为eventsDeleted的节点。

这是删除节点的代码:

removeEvent(eventId, groupId) {

  return new Promise((resolve, reject)=> {

    this.eventRef.child(groupId).child(eventId).remove();
    resolve();

  });

}

这是创建节点的代码:

addEvent(data:any) {
    console.log('Form data', data.group);

    let localEventRef = firebase.database().ref('events').child(data.group.split(',')[1]);
    let storageRef = firebase.storage().ref();
    let file = data.image;
    let uploadTask = storageRef.child('eventImages/' + UuidGenerator.generateUUID()).put(file);
    uploadTask.on('state_changed', function (snapshot) {

    }, function (error) {
        // Handle unsuccessful uploads
        console.error(error);
    }, function () {
        // Handle successful uploads on complete
        let downloadURL = uploadTask.snapshot.downloadURL;
        let keyOfNewEvent = localEventRef.push(
            new Event(
                null,
                firebase.app().auth().currentUser.uid,

                data.description,


                data.location.address,
                0

            )
        ).key;
        localEventRef.child(keyOfNewEvent).update({eventId: keyOfNewEvent});
    });


}

不要介意上传图片的代码。我只需要一种方法来复制整个节点,然后将其粘贴到数据库中的某个位置。提前谢谢。

1 个答案:

答案 0 :(得分:0)

当用户点击删除时,请确保获取要删除的对象,如果您要在数据库中查询该对象,则可以使用removeEvent检索对象,否则您可以直接跳转到localEventRef.child(keyOfEvent).once("value", function(snapshot) { //one data is returned, you can then call the removeEvent fn let eventToBeRemoved = snapshot.val(); //assuming you have the eventid, groupid then. removeEvent(eventId, groupId, eventToBeRemoved); }); removeEvent(eventId, groupId, eventObjectToBeRemoved) { //firebase 3.x comes with a promise method firebase.database().ref('eventsDeleted/' + groupId + '/' + eventId ) .set({...eventObjectToBeRemoved}) .then(function () { eventRef.child(groupId).child(eventId).remove();//you can now remove }) .catch(function (error) { console.error("error occured trying to add to deletedEvents", error); }); }); } 函数。

const Component1 = (props) => <div>My component 1</div>;
const Component2 = (props) => <div>My component 2</div>;
const Component3 = (props) => <div>My component 3</div>;

// associate the item id with 
// the corresponding component
const components = {
  1: <Component1 />,
  2: <Component2 />,
  3: <Component3 />,
}

class MyApp extends Component {

  constructor(props) {
    super(props);
    this.state = { selectedItem: 1 }
  }

  handleItemClick(itemId) {
    this.setState({ selectedItem: itemId });
  }

  render() {
    return (
      <div>
        <li onClick={() => this.handleItemClick(1)}>Item 1</li>
        <li onClick={() => this.handleItemClick(2)}>Item 2</li>
        <li onClick={() => this.handleItemClick(3)}>Item 2</li>
        {components[this.state.selectedItem]}
      </div>
    );
  }
}