简体   繁体   English

我希望能够在我的 firebase 数据库中获取自动生成的 id 子值,并在 function 中使用它们

[英]I want to be able to get the auto generated id child values in my firebase database and use them within a function

For clarity, this is what I mean when I'm referring to the auto generated id in Firebase:为清楚起见,当我指的是 Firebase 中的自动生成 id 时,这就是我的意思:

**-M3pezrJXXdnPVq53M8A**
description: ''

heading: ''

location: ''  

I'm using React Native and Firebase database.我正在使用 React Native 和 Firebase 数据库。 The aim is to update the fields of the selected post.目的是更新所选帖子的字段。 I have passed the values of the post's fields through navigation and want to call the Firebase update function in my 'Update Post' screen.我已经通过导航传递了帖子字段的值,并想在我的“更新帖子”屏幕中调用 Firebase 更新 function。

This is the editing screen.这是编辑画面。 The params are passed over successfully.参数成功传递。

import React from 'react';
import {
  StyleSheet,
  Button,
  TextInput,
  View,
  Text,
  TouchableOpacity,
} from 'react-native';
import {Formik} from 'formik';
import {styles} from './PostList';
import Firebase from 'firebase';
import 'firebase/database';

export default function EditForm({route}) {
  const post = route.params;
  const postKey = post.post.key;
  console.log(postKey);

  function updatePost(values) {
    const ref = Firebase.database()
      .ref('posts')
      .child('key/' + postKey);
    ref
      .update({
        heading: values.heading,
        description: values.description,
        location: values.location,
      })
      .then(snapshot => {
        values.id = snapshot.id;
        snapshot.set(values);
      });
  }

  return (
    <View style={styles.container}>
      <Formik
        initialValues={{
          heading: post.heading,
          description: post.description,
          location: post.location,
        }}
        mapPropsToValues={post => ({
          heading: post.heading,
          description: post.description,
          location: post.location,
        })}
        enableReinitialize={true}
        onSubmit={(values, post) => {
          console.log(values);
          updatePost({
            heading: values.heading,
            description: values.description,
            location: values.location,
          });
        }}>
        {props => (
          <View>
            <TextInput
              style={formikstyles.txtInput}
              placeholder={'Give your post a title'}
              onChangeText={props.handleChange('heading')}
              value={props.values.heading}
            />
            <TextInput
              style={formikstyles.txtInput}
              placeholder={'Tell us about your leftovers...'}
              onChangeText={props.handleChange('description')}
              value={props.values.description}
            />
            <TextInput
              style={formikstyles.txtInput}
              placeholder={'Where can we get our grub?'}
              onChangeText={props.handleChange('location')}
              value={props.values.location}
            />
            <TouchableOpacity
              style={formikstyles.button}
              title={'submit'}
              onPress={props.handleSubmit}>
              <Text style={formikstyles.btnText}>Submit</Text>
            </TouchableOpacity>
          </View>
        )}
      </Formik>
    </View>
  );
}

I've tried to pull the id (postKey) using.key but it reads undefined.我尝试使用.key 提取 id (postKey),但它读取为未定义。 Is there any way of retrieving this parameter?有没有办法检索这个参数?

Edited my addPost function to manually generate a unique id then stored that uid in a field named 'id' in the database.编辑了我的 addPost function 以手动生成唯一 ID,然后将该 uid 存储在数据库中名为“id”的字段中。

export function addPost(values, addComplete) {
  const key = Firebase.database()
    .ref('posts')
    .push().key;
  return Firebase.database()
    .ref('posts/' + key)
    .set({
      id: key,
      heading: values.heading,
      description: values.description,
      location: values.location,
    })
    .then(snapshot => {
      values.Id = snapshot.Id;
      snapshot.set(values);
    })
    .then(() => addComplete(values))
    .catch(error => console.log(error));
}

Now able to access the uid through navigation and map onto the path reference to get the specified post object:现在可以通过导航和 map 访问 uid 到路径引用以获取指定的帖子 object:

import React from 'react';
import {
  StyleSheet,
  Button,
  TextInput,
  View,
  Text,
  TouchableOpacity,
} from 'react-native';
import {Formik} from 'formik';
import {styles} from './PostList';
import Firebase from 'firebase';
import 'firebase/database';

export default function EditForm({route}) {
  const post = route.params;
  const postKey = post.post.id;
  const ref = Firebase.database().ref('posts/' + postKey);

  function updatePost(values) {
    ref
      .update({
        heading: values.heading,
        description: values.description,
        location: values.location,
      })
      .then(snapshot => {
        values.id = snapshot.id;
        snapshot.set(values);
      });
  }

  return (
    <View style={styles.container}>
      <Formik
        initialValues={{
          heading: post.heading,
          description: post.description,
          location: post.location,
        }}
        mapPropsToValues={post => ({
          heading: post.heading,
          description: post.description,
          location: post.location,
        })}
        enableReinitialize={true}
        onSubmit={(values, post) => {
          console.log(values);
          updatePost({
            heading: values.heading,
            description: values.description,
            location: values.location,
          });
        }}>
        {props => (
          <View>
            <TextInput
              style={formikstyles.txtInput}
              placeholder={'Give your post a title'}
              onChangeText={props.handleChange('heading')}
              value={props.values.heading}
            />
            <TextInput
              style={formikstyles.txtInput}
              placeholder={'Tell us about your leftovers...'}
              onChangeText={props.handleChange('description')}
              value={props.values.description}
            />
            <TextInput
              style={formikstyles.txtInput}
              placeholder={'Where can we get our grub?'}
              onChangeText={props.handleChange('location')}
              value={props.values.location}
            />
            <TouchableOpacity
              style={formikstyles.button}
              title={'submit'}
              onPress={props.handleSubmit}>
              <Text style={formikstyles.btnText}>Submit</Text>
            </TouchableOpacity>
          </View>
        )}
      </Formik>
    </View>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM