Query Draft results in undefined is not an object

,

I’m working on processing multiple drafts based on a query but I am stuck on “TypeError: undefined is not an object” when referencing a list of drafts. I have tried to query a Workspace and query the Draft object.

const queryString = "example";
const drafts = Draft.query(queryString);

drafts.forEach( function(d) {
  d.content = d.content.replaceAll(queryString, "");
  d.update();
});
1 Like

I think query() has more than just the one mandatory parameter. Try updating that and check the count of objects in the array - and that an array is being returned.

1 Like

sylumer is right, but the documentation does not make it clear how many of the parameters are mandatory. I think “filter” is mandatory and the ones from “omitTags” on are optional, but I’m not sure whether or not the “tags” parameter is required.

1 Like

@sylumer @FireLarch Yes. Two parameters are required. I passed “inbox” for the filter and the query contained an array of objects. Thank you for your help!

const queryString = "example";
const filter = "inbox";
const drafts = Draft.query(queryString, filter);

drafts.forEach( function(d) {
  d.content = d.content.replaceAll(queryString, "");
  d.update();
});
1 Like

In the docs, a question mark in the parameter definition marks the parameter as optional and TypeDoc then flags it as optional like this.

None of the parameters for query are marked as optional. I have therefore always specified all of the parameters when using it.

Could the documentation be wrong? Yes, and given this started working, I assume there is some defaulting going on. If they are explicit, great. If they are implicit, then it is debatable and the parameters should not be considered optional.

The documentation may be incorrect for the query function, but I think the documentation is pretty clear for anything that is being marked as optional.

1 Like

According to the documentation, sortDescending and sortFlaggedToTop defaults to false. It would seem that tags and omitTags defaults to [] and by providing a default value it is implicit that the parameters are optional.

I’ll take a look at the docs this week and clarify those parameters. You should have at least gotten a log entry in the Action Log indicating bad parameters, I believe.

@agiletortoise I checked the Action Log and there’s a reference to invalid parameters and the documentation.

Draft.query: Invalid parameters, please refer to documentation
Script Error: TypeError: undefined is not an object (evaluating ‘drafts’)