尝试使用 javaScript 在 FaunaDB 中的一个集合中获取所有文档时遇到的问题



我创建了数据库和一个集合。现在我想从这个集合中检索所有文档。

我使用了这个查询方法:

q.Map(
q.Paginate(Documents(Collection('posts'))),
q.Lambda(x => q.Get(x))
).then (x=>console.log(x))

但是每次我运行此代码时,它都会向我发送一条错误消息,告知未定义文档。 谁能告诉我问题是什么?

你在 Map、Paginate 和 Lambda 前面使用 q.。我认为这意味着您没有执行以下操作

import faunadb from 'faunadb'
const q = faunadb.query
const { Documents, Paginate, Collections, Lambda, Get } = q

如果你这样做了,这应该有效。(免责声明:确保不要覆盖特定于语言的函数,如 Map 和/或 Function,对于这些函数,请使用 q.Function 和 q.Map。

另一种方法是将q放在所有内容之前。

q.Map(
q.Paginate(q.Documents(q.Collection('posts'))),
q.Lambda(x => q.Get(x))
).then (x=>console.log(x))

如前所述,如果您确实公开了函数(我个人觉得很方便(,请不要使用 Map 进行操作,因为这是一个 JavaScript 关键字,所以你能得到的最好的是:

import faunadb from 'faunadb'
const q = faunadb.query
const { Documents, Paginate, Collections, Lambda, Get } = q
q.Map(
Paginate(Documents(Collection('posts'))),
Lambda(x => Get(x))
).then (x=>console.log(x))

最新更新