简体   繁体   English

如何将 Firestore 文档引用存储为 nextjs 中的字段?

[英]How do I store a Firestore document reference as a field from nextjs?

Im creating simple blog posts and trying to connect the post to the logged in user.我正在创建简单的博客帖子并尝试将帖子连接到登录用户。 When i create a document reference to be stored as a field with reference type, I get a map as shown below:当我创建一个文档引用以存储为具有引用类型的字段时,我得到一个 map,如下所示:

firestore 数据库显示用户地图而不是参考

Here is what I tried这是我试过的

The logged in user is stored in context and the data is sent to an api route along with user as a reference that already exists in the database:登录用户存储在上下文中,数据与用户一起发送到 api 路由,作为数据库中已存在的参考:

import {useAuth} from '../../context/AuthContext';

page function() {
  const {user} = useAuth();
  const onSubmit = async () => {
    const { title, body } = content;
    await axios.post('/api/post', {title, slug: dashify(title), body, author: doc(db, 'users/' + user.uid)
    setContent({title: '', content: ''}) 
  }
}

the api code is as follows api代码如下

const handler = async (req, res) => {
    try {
        const posts = await getDocs(postsRef);
        const postsData = posts.docs.map((post) => post.data());
        if (postsData.some((post) => post.slug == "slug")) res.status(406).end();
        else {
            const newPost = await addDoc(collection(db, 'posts'), {
                ...req.body,
                createdAt: serverTimestamp(),
            });
            log(newPost, "post details");
            res.status(200).json({ newPost });
        }
        // res.status(201).json({ author });
    } catch (e) {
        log(e, "error occured post");
        res.status(400).end();
    }
};

export default handler;

Instead of passing a DocumentReference directly from frontend, try passing the document path and then create a DocumentReference object on server side as shown below:不要直接从前端传递DocumentReference ,而是尝试传递文档路径,然后在服务器端创建一个DocumentReference object,如下所示:

// API request
await axios.post('/api/post', {
  title,
  slug: dashify(title),
  body,
  author: `users/${user.uid}`
})
// Handler
const newPost = await addDoc(collection(db, 'posts'), {
  ...req.body,
  author: doc(db, req.body.author)
  createdAt: serverTimestamp(),
});

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

相关问题 如何从 Firestore 获取对文档数组的引用 - How to Get Reference to Document Array from Firestore 如何从 Firestore 获取文档 ID 列表 - How do I get a list of document ID’s from Firestore 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript? 如何将来自 Firestore 的字段值存储在 Flutter 中? - How to store field value from firestore in Flutter? 如何使用 Cloud firestore 中的文档 ID 访问文档字段? - How can I acess a document field using document id from cloud firestore? 如何获取在 flutter 中具有特定字段值的 Firestore 文档的 DocumentID? - How do I get the DocumentID of a firestore document that has a particular field value in flutter? 如何通过 flutter 中的文档从 firebase 存储中检索数据? - How do i retrieve data from firebase store by document in flutter? 如何使用帮助文档 ID 更新 Cloud firestore 文档字段? - How can i Update Cloud firestore document Field with the help DocumentID? 如何从 Firestore 文档中获取嵌套参考数据? - How to get nested reference data at ones from Firestore document? 我正在尝试从 ReactJS 更新 Firestore 文档中的字段 - I am trying to update a field inside a document in Firestore from ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM