Search code examples
javascriptlinuxgnomegiogjs

JS ERROR: Error: Expected an object of type GCancellable for argument 'cancellable' but got type string


I'm trying to follow this tutorial. Basically, I want to create my custom function that creates a folder if it not exists.

var makeDir = (path) => {
    const file = Gio.file_new_for_path(path);

    if (file.query_exists(path)) {
        print(`Dir already exists ${path}`);
        return;
    }

    print(`My Path: ${path}`);

    // file.make_directory(path);
};

When I run this code I'm receiving an error:

Gjs-CRITICAL **: 17:35:17.161: JS ERROR: Error: Expected an object of type GCancellable for argument 'cancellable' but got type string

In the documentation I see that GCancellable is optional. So I have no idea why my code does not work. Haw can I make it work?


Solution

  • In the C documentation, "optional" means something else than it usually does in JS: it means it's optional to pass a real pointer as that argument, and you may also pass NULL.

    The error message is complaining about a string because query_exists() does not take a path string argument. Check the JS documentation for the list of arguments accepted in JS: you should call file.query_exists(null).