Javascript Firebase错误函数CollectionReference.doc()

Javascript Firebase错误函数CollectionReference.doc(),javascript,reactjs,firebase,google-cloud-firestore,Javascript,Reactjs,Firebase,Google Cloud Firestore,react中与firebase连接的以下项目存在以下问题代码。单击enter按钮提交消息后,它会从数据库中返回错误。(下图)。我创建了集合“rooms”,并尝试使用db.collection(“rooms”).doc(channelId).collection({})第17行访问,这将从firebase返回错误 此错误消息意味着什么? FirebaseError:函数CollectionReference.doc()要求其第一个参数的类型为非空字符串,但它是:true 我删除了firebase.

react中与firebase连接的以下项目存在以下问题代码。单击enter按钮提交消息后,它会从数据库中返回错误。(下图)。我创建了集合“rooms”,并尝试使用db.collection(“rooms”).doc(channelId).collection({})第17行访问,这将从firebase返回错误

此错误消息意味着什么? FirebaseError:函数CollectionReference.doc()要求其第一个参数的类型为非空字符串,但它是:true

我删除了firebase.ps1文件并重新登录。 创建了一个新的db.collection,但仍然相同

import React,{useState}来自“React”;
导入“/ChatInput.css”;
从“/firebase”导入数据库;
从“/StateProvider”导入{useStateValue};
从“firebase”导入firebase;
从“@material ui/core”导入{Button}”;
函数输入({channelName,channelId}){
const[input,setInput]=useState(“”);
const[{user}]=useStateValue();
const sendMessage=(e)=>{
e、 预防默认值();
if(channelId){
db.集合(“房间”).doc(channelId).集合({
信息:输入,
时间戳:firebase.firestore.FieldValue.serverTimestamp,
用户:user.displayName,
userImage:user.photoURL,
});
}
设置输入(“”);
};
返回(
setInput(e.target.value)}
占位符={`Message}${channelName?.toLowerCase()}`}/>
发送
);
}
导出默认输入
此错误消息意味着什么?FirebaseError:函数
CollectionReference.doc()要求其第一个参数的类型为
非空字符串,但它是:true

这意味着您需要传递一个字符串作为方法的参数

您正在将
channelId
传递给该方法,它似乎是一个值为
true
的布尔值。您需要传递一个表示Firestore文档ID的字符串。我猜是
channelName
,不是吗


此外,代码中还有另一个问题:将JavaScript对象传递给该方法


例如,如果要“提交消息”,则需要调用
CollectionReference
上的方法

类似于以下内容(我们不知道您要使用的子集合名称,让我们记录“消息”):


我建议你看一下医生:

此错误消息意味着什么?FirebaseError:函数 CollectionReference.doc()要求其第一个参数的类型为 非空字符串,但它是:true

这意味着您需要传递一个字符串作为方法的参数

您正在将
channelId
传递给该方法,它似乎是一个值为
true
的布尔值。您需要传递一个表示Firestore文档ID的字符串。我猜是
channelName
,不是吗


此外,代码中还有另一个问题:将JavaScript对象传递给该方法


例如,如果要“提交消息”,则需要调用
CollectionReference
上的方法

类似于以下内容(我们不知道您要使用的子集合名称,让我们记录“消息”):


我建议您查看一下文档:

importreact,{useState}来自“React”;
导入“/ChatMsg.css”;
从“/firebase”导入数据库;
从“firebase”导入firebase;
从“/stateProvider”导入{useStateValue};
函数ChatMsg({channelName,channelId}){
const[input,setInput]=useState(“”);
const[{user}]=useStateValue();
const sendMessage=(e)=>{
e、 预防默认值();
if(channelId){
db.collection(“房间”).doc(channelId)。collection(“消息”)。添加({
信息:输入,
用户:user.displayName,
时间戳:firebase.firestore.FieldValue.serverTimestamp(),
userimage:user.photoURL
});
}
};
返回(
setInput(e.target.value)}
占位符={`Message}${channelName}}
/>
发送
);
}
导出默认ChatMsg;
导入React,{useState}来自“React”;
导入“/ChatMsg.css”;
从“/firebase”导入数据库;
从“firebase”导入firebase;
从“/stateProvider”导入{useStateValue};
函数ChatMsg({channelName,channelId}){
const[input,setInput]=useState(“”);
const[{user}]=useStateValue();
const sendMessage=(e)=>{
e、 预防默认值();
if(channelId){
db.collection(“房间”).doc(channelId)。collection(“消息”)。添加({
信息:输入,
用户:user.displayName,
时间戳:firebase.firestore.FieldValue.serverTimestamp(),
userimage:user.photoURL
});
}
};
返回(
setInput(e.target.value)}
占位符={`Message}${channelName}}
/>
发送
);
}
导出默认ChatMsg;

是的,这正是我要做的,对于子集合“messages”,添加缺少的参数后,我仍然会得到指向同一行的相同消息。我在问题“更改”中添加了图片“更改2”嗨,卢卡斯,你有机会看一下更新吗?如果答案能解决你的问题,请接受。谢谢你,我做了,但最后没有什么可以接受的。。。我向上投了票,但(你的意思是什么?)只有当我再达到14分时,它才会显示出来……是的,这正是我试图做的,在子集合“消息”中,添加缺少的参数后,我仍然会得到相同的消息
db.collection("rooms").doc(channelName).collection('messages')
  .add({
       message: input,
       timestamp: firebase.firestore.FieldValue.serverTimestamp,
       user: user.displayName,
       userImage: user.photoURL,
  }); 
import React, { useState } from "react";
import "./ChatMsg.css";
import db from "./firebase";
import firebase from "firebase";
import { useStateValue } from "./stateProvider";

function ChatMsg({ channelName, channelId }) {
  const [input, setInput] = useState("");
  const [{ user }] = useStateValue();
  const sendMessage = (e) => {
    
    e.preventDefault();
    if (channelId) {
      db.collection("rooms").doc(channelId).collection('messages').add({
        message: input,
        user: user.displayName,
        timestamp: firebase.firestore.FieldValue.serverTimestamp(),
        userimage:user.photoURL
        
       
      });
            
    }
  };

  return (
    <div className="chatInput">
      <form>
        <input
          value={input}
          onChange={(e) => setInput(e.target.value)}
          placeholder={`Message # ${channelName}`}
        />
        <button type="submit" onClick={sendMessage}>
          SEND
        </button>
      </form>
    </div>
  );
}
export default ChatMsg;