You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中Firebase Realtime Database update无法识别onPress传入参数

问题根因

你写的{Attribute : this.state.inputText}属于JavaScript对象字面量的基础语法问题:对象的键名如果不做特殊处理,会被默认解析为字符串"Attribute",而不是你传入的Attribute参数变量,所以数据库更新的时候永远只会写死更新Attribute字段,不会识别你传的'Class'参数。

修复方案

把对象的键名用方括号包裹,使用计算属性名语法,让JS把方括号内的内容作为变量解析,取变量的值作为最终的键名。

修复后的完整onSubmit代码:

onSubmit(Attribute){
  this.setState({
    apply:false,
  })
  const ref = database().ref('Users/Chase/Ivy')
  // 仅修改update的参数对象,用[]包裹Attribute变量
  ref.update({[Attribute] : this.state.inputText}).then(()=> {
      console.log('updated!!!');
    }).catch((error) => {
      console.log(error);
    })
}

验证效果

修改后调用this.onSubmit('Class')时,会正确把数据库中Class字段的值更新为你存在this.state.inputText里的内容。

内容的提问来源于stack exchange,提问作者valor323

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 04:06:03