Hi! Beginner question here. I want to write a new function that only returns the todos authored by a specific author. An example of one of my documents looks like:
{
"ref": Ref(Collection("todos"), "2683675425588801549"),
"ts": 1592194937000000,
"data": {
"description": "some description",
"url": "https://www.faunadb.com",
"location": "Everywhere",
"tags": "fql",
"completed": false,
"author": "auth0|5ee2f6049b240a0013247655"
}
}
I’m following the official Netlify-FaunaDB tutorial, so I see that an example of querying ALL of the todos in the todos-read-all.js
file looks like:
client
.query(q.Paginate(q.Match(q.Ref("indexes/all_todos"))))
.then((response) => {
… and that indexes/all_todos
comes from the boostrap-fauna-database.js
file here:
return client.query(q.Create(q.Ref('classes'), { name: 'todos' }))
.then(() => {
return client.query(
q.Create(q.Ref('indexes'), {
name: 'all_todos',
source: q.Ref('classes/todos')
}))
}).catch((e) => {
// Database already exists
if (e.requestResult.statusCode === 400 && e.message === 'instance not unique') {
console.log('Fauna already setup! Good to go')
console.log('Claim your fauna database with "netlify addons:auth fauna"')
throw e
}
})
}
In order to get only the TODOs created by a specific author, this is what I tried:
/* Import faunaDB sdk */
const faunadb = require("faunadb");
const getId = require("./utils/getId");
const q = faunadb.query;
exports.handler = (event, context) => {
console.log("Function `todo-read-all-profile` invoked");
/* configure faunaDB Client with our secret */
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET,
});
const userId = getId(event.path);
return client
.query(q.Paginate(q.Equals(data.author, userId)))
.then((response) => {
and
const readProfile = (userId) => {
return fetch(`/.netlify/functions/todos-read-all-profile/${userId}`, {
method: "GET",
}).then((response) => {
return response.json();
});
};
But the problem, of course, is that .query(q.Paginate(q.Equals(data.author, userId)))
I was trying in that todos-read-all-profile.js
file doesn’t work.
There is no reference to data
, so data
is undefined. Does anyone know how I can actually “get” at the data object in the document?
That is, I want to filter all the documents to return ONLY the ones where data. author === userId.
Thanks in advance for any help!