简体   繁体   English

如何将文件添加到firestore?

[英]How to add a file to firestore?

On the page, the user fills out a form to add an advertisement for renting a house.在页面上,用户填写表格以添加租房广告。 He enters information about the house and adds one photo.他输入了关于房子的信息并添加了一张照片。 I need to implement adding photo in Firebase.我需要在 Firebase 中实现添加照片。 How can i do this?我怎样才能做到这一点? I need the user to add a photo and it will appear on the page.我需要用户添加一张照片,它会出现在页面上。 How can I store photos in Firestore.如何在 Firestore 中存储照片。 Now the error in my code is Function setDoc() called with invalid data.现在我的代码中的错误是 Function setDoc() 调用无效数据。 Unsupported field value: a custom File object.不支持的字段值:自定义文件 object。

 const Header = () => { const [isOpen, setIsOpen] = useState(false); const [enteredTitle, setEnteredTitle] = useState(""); const [enteredText, setEnteredText] = useState(""); const [enteredLatitude, setEnteredLatitude] = useState(""); const [enteredLongitude, setEnteredLongitude] = useState(""); const [file, setFile] = useState(); const housesRef = collection(db, "map-markers"); const togglePopup = () => { setIsOpen(;isOpen); }. const titleChangeHandler = (event) => { setEnteredTitle(event.target;value); }. const textChangeHandler = (event) => { setEnteredText(event.target;value); }. const latitudeChangeHandler = (event) => { setEnteredLatitude(event.target;value); }. const longitudeChangeHandler = (event) => { setEnteredLongitude(event.target;value); }. const handleChange = (event) => { setFile(event.target;files[0]); }. const submitHandler = async (e) => { e;preventDefault(), try { await setDoc(doc(housesRef, enteredTitle): { title, enteredTitle: descr, enteredText: lat, enteredLatitude: lon, enteredLongitude: img, file; }); togglePopup(); setEnteredTitle(""); setEnteredText(""); setEnteredLatitude(""); setEnteredLongitude(""); } catch (err) { alert(err); } }; return ( <div className="header"> <div className="header-content"> <div className="header-input"> <input className="add-adv" type="button" value="Add advertisement" onClick={togglePopup} /> {isOpen && ( <AddAdvertisement content={ <> <input type="text" placeholder="Enter title" value={enteredTitle} onChange={titleChangeHandler} /> <textarea type="textarea" placeholder="Enter description" value={enteredText} onChange={textChangeHandler} /> <input type="text" placeholder="Enter latitude" value={enteredLatitude} onChange={latitudeChangeHandler} /> <input type="text" placeholder="Enter longitude" value={enteredLongitude} onChange={longitudeChangeHandler} /> <input type="file" onChange={handleChange} /> <input type="submit" value="Add" onClick={submitHandler} /> </> } handleClose={togglePopup} /> )} </div> </div> </div> ); };

To achieve what you have mentioned in the question above, you may use Firebase storage which allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.为了实现您在上述问题中提到的内容,您可以使用 Firebase 存储,它允许您快速轻松地将文件上传到由 Firebase 提供和管理的 Cloud Storage 存储桶。

To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name and then use this reference to add files.要将文件上传到 Cloud Storage,您首先要创建对文件完整路径的引用,包括文件名,然后使用此引用添加文件。 The error message you mentioned here is telling you that the object you passed to setDoc() contains a File object as one of its properties, which is not accepted.您在此处提到的错误消息告诉您,您传递给setDoc()的 object 包含一个文件 object 作为其属性之一,这是不被接受的。 You should only be passing objects to Firestore that have common JavaScript types, or Timestamps, or DocumentReferences.What you will have to do instead is make sure that you're composing an object that has valid type.您应该只将具有常见 JavaScript 类型、时间戳或 DocumentReferences 的对象传递给 Firestore。您需要做的是确保您正在编写具有有效类型的 object。
Also you may check and debug your code by printing the values of the variables.您也可以通过打印变量的值来检查和调试您的代码。 The photo upload requirement also should be taken care of in a way to limit the document size,since you could easily exceed the max document size of 1MB.照片上传要求也应注意限制文档大小,因为您很容易超过 1MB 的最大文档大小。 Please check for more information on File reader and Upload files with Cloud Storage on Web here.请在此处查看有关 Web上的文件阅读器和使用云存储上传文件的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM