【发布时间】:2020-12-24 19:34:30
【问题描述】:
我有以下代码:
import { DocumentReference } from '@firebase/firestore-types'
export type Recipe = {
author: string
title: string
ingredients: {
quantity: number
ingredient: DocumentReference["path"]
}[]
instructions: string[]
}
export const getAllRecipes = async() => {
const snapshot = await db.collection('recipes').get()
const data = snapshot.docs.map(doc => doc.data())
return data
}
const recipes: Recipe[] = await getAllRecipes()
由于某种原因,我似乎无法找到适合配料的类型。 我得到的错误是:
Error: Error serializing .recipes[0].ingredients[0].ingredient returned from getStaticProps in "/".
在firestore 中的typescript 中定义引用数组类型的方法是什么?
另外,如果我这样做(只是为了调试)
export const getAllRecipes = async() => {
const snapshot = await db.collection('recipes').get()
const data = snapshot.docs.map(doc => {
const docData = doc.data()
const {ingredients} = docData
ingredients.map((ingredient: DocumentReference) => {
const jsonIngredient = ingredient.path
console.log(jsonIngredient)
})
return docData
})
return data
}
【问题讨论】:
-
哪行代码会产生该错误?我根本看不到任何序列化任何东西的代码。我也看不到您在哪里使用“食谱”类型。我建议编辑问题以添加所有相关代码,并解释您希望代码做什么而不是生成此错误。
-
@DougStevenson 添加了更多代码。但是,可以只是一个定义类型的简单调用,不需要写出来,因为已经有一个调用函数。例如 const 食谱:Recipes[] = await getAllRecipes()
-
这是您正在运行的所有代码吗?我仍然没有看到任何序列化。哪一行代码导致错误?
-
@DougStevenson 最后一行,注意标签包含 Nextjs,所以错误来自调用 await getAllRecipes() 并从 getStaticProps 序列化。我更新了完整的错误消息
-
您的文档是什么样的?那将是实际数据,对吧?
标签: typescript firebase google-cloud-firestore next.js