0
Follow
0
View

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

cyl5331113 注册会员
2023-01-25 15:43

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:

// 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(),
});