How can I see if a document exists from a Index?

app.get('/start', async (req, res) => {
    const start = Date.now();
    const userExists = await db.query(
        Exists(
            Ref(Collection('users'), Match(Index('getUserById'), req.author.id))
        )
    );
    if (userExists) {
        res.send('You alreay have a account!');
        return;
    }
    db.query(Create(Collection('users'), { data: { id: req.author.id } })).then(
        () => {
            res.send(`Finished, it took me ${Date.now() - start}ms!`);
        }
    );
});

I keep getting this error in my express app:
description: 'String or Number expected, Set provided.'

I’m new to faunadb and am transitioning from firebase, help is much appreciated

Match(Index('getUserById'), req.author.id) is going to return a Set containing 0 or 1 references (I’m guessing based on your code) to documents in Collection('users'). There’s no need to build a Reference yourself, since Set is already returning them for you. And creating the reference the way you did doesn’t make sense anyways, since it requires a string or a number, but the Match operation returns a Set (hence the error). Exists() checks for both Document existence and whether Sets are empty or not, so this code should work:

app.get('/start', async (req, res) => {
    const start = Date.now();
    const userExists = await db.query(
        Exists(
            Match(Index('getUserById'), req.author.id)
        )
    );
    if (userExists) {
        res.send('You alreay have a account!');
        return;
    }
    db.query(Create(Collection('users'), { data: { id: req.author.id } })).then(
        () => {
            res.send(`Finished, it took me ${Date.now() - start}ms!`);
        }
    );
});

But beyond that, there’s no need to split up the two queries, you can do both of them together in a single transaction using code like this:

Let({
  userExists: Exists(
    Match(Index('getUserById'), req.author.id)
  )
},
  If(
    Var('userExists'),
    Abort('You already have an account!'),
    Create(Collection('users'), { data: { id: req.author.id } })
  )
)
2 Likes

Thanks Wallslide!!! totally new to serverless / faunadb - combining the two queries like that is a nice touch :partying_face: :+1:t5:

1 Like
const faunadb = require("faunadb");
const q = faunadb.query;
require("dotenv").config();

exports.handler = async (event, context) => {
  const client = new faunadb.Client({
    secret: process.env.FAUNA_SECRET_KEY,
  });

  const userId = event.queryStringParameters.id;

  return client
    .query(
      q.Let(
        {
          userExists: q.Exists(q.Match(q.Index("user_by_ntl_id_2"), userId)),
        },
        q.If(
          q.Var("userExists"),
          q.Abort("-- Account Already Exists --"),
          q.Create(q.Collection("Users"), { data: { id: userId } })
        )
      )
    )
    .then((response) => {
      console.log("success", response);
      console.log(context.clientContext.user);
      return {
        statusCode: 200,
        body: JSON.stringify(response),
      };
    })
    .catch((error) => {
      console.log("error", error);
      return {
        statusCode: 400,
        body: JSON.stringify(error),
      };
    });
};

This works - I am using Netlify for authentetication , currently refactoring it.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.