JS 错误:错误:参数“cancellable”需要类型为 GCancellable 的对象,但得到类型字符串

问题描述

我正在尝试关注此 tutorial。 基本上,我想创建我的自定义函数,如果它不存在则创建一个文件夹。

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);
};

当我运行此代码时,我收到一个错误

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

documentation 中,我看到 GCancellable 是可选的。所以我不知道为什么我的代码不起作用。我怎样才能让它工作?

解决方法

在 C 文档中,“可选”的意思不同于它在 JS 中通常所做的:这意味着传递一个真正的指针作为该参数是可选的,您也可以传递 NULL

错误消息抱怨字符串,因为 query_exists() 不接受 path 字符串参数。检查 JS documentation 以获取 JS 接受的参数列表:您应该调用 file.query_exists(null)