How to update a Fauna DB value via an Update call

I have a ReactJS NextJS web-app . The following code outputs the correct values to the terminal however it fails to update the database at all. As such, I think that the faunaDB Update function is incorrectly specified. All 4 console.log calls are outputting the correct values.

const faunadb = require("faunadb");

// your secret hash
const secret = process.env.FAUNADB_SECRET_KEY;
const q = faunadb.query;
const client = new faunadb.Client({ secret });

console.log("CALLLED API")

module.exports = async (req, res) => {
  const formData = req.body;
  console.log("API CALL");
  try {
    const dbs = await client.query(
      console.log("API UPDATE"),
      console.log(formData.likes[0].data.like + 1),

      q.Update(
        q.Ref(q.Collection('Likes'), '__likes_reference__'),
        {
          data: {
            like: formData.likes[0].data.like + 1,
          },
        }
      )
    );
    // ok
    res.status(200).json(dbs.data);
  } catch (e) {
    // something went wrong
    res.status(5
00).json({ error: e.message });
  }

};

How can I increment the value for likes in the database?

What are you getting back as your error message?

Aside from the console.logs which are all outputting the expected values on the terminal, the browser console is throwing a 500 error.

Hi @winterdelta and welcome!

You cannot include console.log as arguments to a query.

You can see in the JS driver docs that client.query expects as arguments:

  1. expression: your FQL expression
  2. options: query options which can include a bunch of things (check the driver docs for more info)

console.log doesn’t return anything (or consider that it returns undefined). client.query doesn’t accept a third argument. So you are essentially calling

client.query(undefined, undefined)

More tips for debugging

I find it useful to log the entire error object to see what fauna sends back.

client
  .query(undefined, undefined)
  .then((res) => console.log(res))
  .catch((e) => console.error(JSON.stringify(e, null, 2)))

// console result:

{
  "name": "BadRequest",
  "message": "invalid expression",
  "description": "Request body is not valid JSON.",
  "requestResult": {
    "method": "POST",
    "path": "",
    "query": null,
    "responseRaw": "{\"errors\":[{\"position\":[],\"code\":\"invalid expression\",\"description\":\"Request body is not valid JSON.\"}]}",
    "responseContent": {
      "errors": [
        {
          "position": [],
          "code": "invalid expression",
          "description": "Request body is not valid JSON."
        }
      ]
    },
    "statusCode": 400,

    /* ... */
  }
}