首页 > 解决方案 > 不断得到:FirebaseError:使用无效数据调用函数 DocumentReference.update()。不支持的字段值:一个函数

问题描述

当他们填写某个 TextInput 时,我正在尝试为某个用户更新一个字段。当我填写表格时,它会更新 Cloud Firestore 中的字段。但是,我不断收到警告或错误:

我收到一个警告:警告:道具类型失败:提供给, 预期value的类型无效的道具。arrayForwardRef(TextInput)string

我有时也会遇到错误:FirebaseError: Function DocumentReference.update() 调用无效数据。不支持的字段值:一个函数

我知道这可能也是数字与字符串的问题,但我尝试将我的值转换为字符串,但似乎没有解决方案可以消除警告或错误。

这是我现在正在使用的代码:

import React, { useState } from 'react'
import { Image, Text, TextInput, TouchableOpacity, View } from 'react-native'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import styles from './styles';
import AsyncStorage from '@react-native-community/async-storage';


import { firebase, firebaseConfig, db } from '../../firebase/config'
import "firebase/auth";
import "firebase/firestore";


export default function MoreInfo({ navigation }) {
    var bp = useState(0)
    var age = useState(0)
    var monthsPreg = useState(0)

    //to properly save data 
    const user = firebase.auth().currentUser;


   
    const onSubmitPress = () => {
        db.collection("users").doc(user.uid).update(
            {
                hasOptedIn: true
            }
        )
       navigation.navigate('Home', { user })

    }
    return (
        <View style={styles.container}>
            <KeyboardAwareScrollView
                style={{ flex: 1, width: '100%' }}
                keyboardShouldPersistTaps="always">
                <Image
                    style={styles.logo}
                    source={require('../../../assets/icon.png')}
                />

                {<TextInput
                    style={styles.input}
                    placeholderTextColor="#aaaaaa"
                    secureTextEntry
                    placeholder='Blood Pressure'
                    keyboardType={'numeric'}
                    input type = "numeric"
                    value={bp}
                    onChangeText={
                        (text) => {
                        
                            const userRef = db.collection("users").doc(user.uid).update(
                              
                                {
                                    "bloodPresure": bp
                                }
                                
                            )
                                .then(function () {
                                    console.log("Document successfully updated!");
                                });

                        }}
                    
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />}
            
                <TouchableOpacity
                    style={styles.button}
                    onPress={() => onSubmitPress()}>
                    <Text style={styles.buttonTitle}>Submit Data</Text>
                </TouchableOpacity>
            </KeyboardAwareScrollView>
        </View>
    )
}

标签: javascriptfirebasereact-nativegoogle-cloud-firestorereact-native-textinput

解决方案


db.collection("users").where("passcode", "==", passcode).limit(1).get().then(query => {
                console.log(query);
                const thing = query.docs[0];
                console.log(thing.data());
                let tmp = thing.data();
                tmp.current_game_play = tmp.current_game_play + 1;
                console.log(tmp);
                thing.ref.update(tmp);
            });

推荐阅读