首页 > 解决方案 > 字符串插值格式在 Firestore 更新功能中不起作用

问题描述

我的目标是将我的type道具传递给一个字符串:

 db.collection("users").doc(user).collection("theme").doc(user).update({
        `button.${type}`: e.target.value,
      });

但是, `button.${type}`: e.target.value,呈现错误Unexpected token (86:8)

有任何想法吗?

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


传递字符串键以访问对象中的变量的正确方法是obj[stringKey]

  { [button[type]]: e.target.value }

你只需要确保它button[type]不是未定义的,否则你的对象将是{ undefined: e.target.value }


推荐阅读