Is there a way for data api to get the notes in the root directory?

As the title says, I want to get the notes of the root directory through the joplin data api and display them. I have investigated the following apis, but have not found a suitable method.

  1. /folders/:id/notes: id doesn't seem to be able to pass a null value
http://localhost:27583/folders//notes?token=
{
"error": "Not Found: \n\nError: Not Found\n    at C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\utils\\defaultAction.js:22:19\n    at Generator.next (<anonymous>)\n    at C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\utils\\defaultAction.js:8:71\n    at new Promise (<anonymous>)\n    at __awaiter (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\utils\\defaultAction.js:4:12)\n    at default_1 (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\utils\\defaultAction.js:20:12)\n    at Object.<anonymous> (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\routes\\folders.js:28:52)\n    at Generator.next (<anonymous>)\n    at C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\routes\\folders.js:8:71\n    at new Promise (<anonymous>)"
}
  1. /search: Didn't find a way to search for top notes
http://localhost:27583/search?query=&type=note&token=

{
"error": "Missing \"query\" parameter: \n\nError: Missing \"query\" parameter\n    at Object.<anonymous> (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\routes\\search.js:25:19)\n    at Generator.next (<anonymous>)\n    at C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\routes\\search.js:8:71\n    at new Promise (<anonymous>)\n    at __awaiter (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\routes\\search.js:4:12)\n    at Object.default_1 [as fn] (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\routes\\search.js:20:12)\n    at Api.<anonymous> (C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\Api.js:155:41)\n    at Generator.next (<anonymous>)\n    at C:\\Users\\rxliuli\\AppData\\Local\\Programs\\Joplin\\resources\\app.asar\\node_modules\\@joplin\\lib\\services\\rest\\Api.js:8:71\n    at new Promise (<anonymous>)"
}

I also checked the forum but found no similar question

Can you even have notes to be stored at the root level? I didn't think you could create a note that didn't have a parent notebook.

In fact, I can pass PUT /notes/:id to pass in parent_id: undefined to modify the note to be a note in the root directory. Currently, an unresolved problem of joplin-vscode-plugin@0.8.0 is that you can drag and drop notes to the root directory, but it cannot be displayed

I wonder if that is a bug, I would have assumed there would be some kind of database constraint to stop you doing that. (It seems there is a constraint to stop it being set to null but it looks like it will allow ""?)

If you look in the database for those notes, what does the notes.parent_id field get set to?

It's a little strange, the joplin on the company's computer really can't set an empty parent_id (to be precise, it is set, but it will not take effect). I go back to try the joplin at home at night, maybe the version is too old. . .

Well I've been playing around with it a bit on 2.8.8 in the background as it has been interesting me.
You can indeed set the folder id to pretty much anything but you can''t retrieve it. Joplin will treat that as a "root" note.

For example you can set it to "", a or testingtesting123 but it will fail to retrieve the ID if you then pass that to the API.

curl -X PUT --data '{"title": "My new title2", "parent_id": "a"}'......

Then:

curl -X GET 'http://localhost:27583/folders/a/notes?token=.....

Results in:

{"error":"Internal Server Error: Cannot read properties of undefined (reading 'id'): \n\n
TypeError: Cannot read properties of undefined (reading 'id')\n
    at Object.<anonymous> (/tmp/.mount_JoplinC9aPam/resources/app.asar/node_modules/@joplin/lib/services/rest/routes/folders.js:34:136)\n
    at Generator.next (<anonymous>)\n
    at fulfilled (/tmp/.mount_JoplinC9aPam/resources/app.asar/node_modules/@joplin/lib/services/rest/routes/folders.js:5:58)\n
    at processTicksAndRejections (internal/process/task_queues.js:95:5)"}⏎  

Unless there is some kind of use case for being able to do this then I think it is something that the API should be checking for and rejecting if you try to set a note to a "fake" folder id (i.e. check that it at least matches the expected format of a folder id).
Maybe it should even go as far to check the folders table for a match when you use POST or PUT for a note - or would that have too much a performance impact on the API?

I would generally check, but this should actually be done in joplin as well, as it would be fatal if it happened -- it would render the database unavailable unless it was fixed manually.

Also I don't know that parent_id can be set to any value, I've only checked recursion issues before, ref: https://github.com/rxliuli/joplin-utils/blob/4bb9a94a2ab26861616652bce9daf1de18d729e4/libs/joplin-api/src/api/FolderExtApi.ts#L42

Sorry, this is exactly what I meant, that the Joplin REST API library itself should be doing this, not clients using it. For example a badly coded plugin could make these changes as the plugin API also uses the data API.

Maybe here should @laurent

We don't check the parent ID because it may or may not have been synced by the time we do the check. As for an empty note parent ID, if it works it's not supported and I'm not sure we'll do something about it at this point. It's possible that there's a use, internally, for notes without parent IDs and reviewing all this and implementing this validation properly is probably not worth it.

I think we can only check the api layer exposed externally, but not internally, because the code inside joplin is controllable, while the external program calling api is not controllable

It would not be as fatal as you say (assuming we're talking of having a note that has invalid parent_id). This happens all the time during sync - some notes can be synced while the corresponding folders hasn't been synced yet. The notes will still be visible in "All notes", can be edited, etc.

Back to the first question, is there a way for me to get the notes in the root directory through the data api? @laurent