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 } })
  )
)
1 Like