javascript - 不断得到:FirebaseError:使用无效数据调用函数 DocumentReference.update()。不支持的字段值:一个函数
问题描述
当他们填写某个 TextInput 时,我正在尝试为某个用户更新一个字段。当我填写表格时,它会更新 Cloud Firestore 中的字段。但是,我不断收到警告或错误:
我收到一个警告:警告:道具类型失败:提供给, 预期value
的类型无效的道具。array
ForwardRef(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>
)
}
解决方案
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);
});
推荐阅读
- python - Python循环内存泄漏
- datatables - jQuery数据表不显示数据库中的数据
- java - 如何解决java中方法的通用输入参数
- c# - 从 DataTable 到 DataGridView 的字符串:“\n”不被解释为新行
- android - 使用签名版本设置 versionCode 的问题
- java - 如何通过 Axios 将标头发送到 Spring-Boot 应用程序?
- deployment - 构建失败 - apksigner.BAT 以代码 2 退出
- reporting-services - Dynamics CRM 在线报告:高级查找视图过滤
- ios - 如何找到 UITableViewCell 的 ViewController?
- java - 为什么循环迭代一次额外的时间?