【问题标题】:firebase realtime database update inside function cant see parameter React Native函数内部的firebase实时数据库更新看不到参数React Native
【发布时间】:2026-02-17 18:50:02
【问题描述】:

所以我试图将参数从 onPress 函数传递到 database.ref().update(parameter here),但更新函数看不到 onPress 参数。这是我的代码。你能帮我弄清楚我做错了什么吗?

onSubmit(属性){ 这个.setState({ 适用:假, }) const ref = database().ref('Users/Chase/Ivy')

  ref.update({Attribute : this.state.inputText}).then(()=> {
      console.log('updated!!!');
    }).catch((error) => {
      console.log(error);
    })
  }

这里是 onPress 内联函数

{(this.state.apply) ?

【问题讨论】:

  • 你没有使用onSubmit的参数,Attribute
  • 我觉得我在 ref.update(Attribute: this.state.input text)...里面是 onSubmit 函数。当我输入代码时,它并没有把它全部放在灰色的框中。抱歉,这是我在这里的第一个问题,我不知道如何格式化我的问题。
  • "Attribute" 是您正在创建的对象的键名。您没有在该函数中使用名为 Attribute 的参数。
  • 我怎样才能使键等于参数属性,因为当我调用该函数时,我想每次传递一个不同的键。并感谢您的帮助
  • 我已经尝试过 .ref(...Atrribute) 似乎可行,但是当我这样做时语法会混乱

标签: react-native firebase-realtime-database parameter-passing


【解决方案1】:

您似乎对 JS 中对象的工作方式感到困惑。

const fn (param) => {
  const obj = {param: "something"};
  const obj2 = {[param]: "beta"};
  const obj3 = {param};

  console.log(obj, obj2, obj3);
};

fn("carlos");

// Outputs:
// {param: "something"}
// {carlos: "beta"}
// {param: "carlos"}

【讨论】: