Back

[SOLVED] Invalid queries with sdk.Query.equal in node.js listDocuments function

  • 0
  • Functions
whataboutno13
24 Mar, 2023, 09:47

Hi fellow appwrite community,

I am getting an Invalid queries error when using the listDocuments function in a node.js function.

The function I use has the following code:

TypeScript
const sdk = require("node-appwrite");
const { databaseId, eventsCollectionId } = require("./constants");

module.exports = async function (req, res) {
  const client = new sdk.Client();

  const database = new sdk.Databases(client);

  if (
    !req.variables['APPWRITE_FUNCTION_ENDPOINT'] ||
    !req.variables['APPWRITE_FUNCTION_PROJECT_ID'] ||
    !req.variables['APPWRITE_FUNCTION_API_KEY']
  ) {
    console.info("Environment variables are not set. Function cannot use Appwrite SDK. Exiting.");
    res.send("Environment variables are not set.", 401);
    return;
  } else {
    client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
      .setSelfSigned(true);
  }

  if (!req.variables['APPWRITE_FUNCTION_USER_ID']) {
    console.info("UserId is not set.");
    res.send("UserId is not set.", 401);
    return;
  }
  let userId = req.variables['APPWRITE_FUNCTION_USER_ID']

  await database.listDocuments(databaseId, eventsCollectionId, sdk.Query.equal("ownerId", userId))
    .then((value) => console.info(value.documents.pop()));
};

This is the error that gets thrown in Appwrite:

TypeScript
Error: Invalid queries: Value must a valid array and Value must be a valid string and no longer than 4096 chars
    at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Databases.listDocuments (/usr/code-start/node_modules/node-appwrite/lib/services/databases.js:1002:16)
    at async module.exports (/usr/code-start/src/index.js:56:3)
    at async /usr/local/src/server.js:68:13

Am I using sdk.Query.equal wrong or what could be the issue here?

TL;DR
The user was facing an "Invalid queries" error when using the `listDocuments` function in a node.js app. They figured out that the issue was with their usage of `sdk.Query.equal` and that they needed to add square brackets around the `Query` call. Solution: Instead of `sdk.Query.equal("ownerId", userId)`, use `[sdk.Query.equal("ownerId", userId)]` in the `listDocuments` function.
whataboutno13
24 Mar, 2023, 10:04

Just solved it myself 😅 you have to add square brackets around the Query call like this. [sdk.Query.equal("ownerId", userId)]

whataboutno13
28 Mar, 2023, 14:47

[SOLVED] Invalid queries with sdk.Query.equal in node.js listDocuments function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more