我正在尝试从node.js在Google云端硬盘中创建一个文件夹,我发现an example here还显示了如何将文件上传到新文件夹中。
我使用TypeScript,我的代码如下:
getAuthorizedClient().then((client) => {
const drive = new drive_v3.Drive({ auth: client });
const folderMetadata = {
'name': `Order_${order.key}`,
'mimeType': 'application/vnd.google-apps.folder'
};
drive.files.create({
resource: folderMetadata,
fields: 'id'
})
.then((folder) => {
console.log('Created folder Id: ', folder.id);
})
.catch(err => console.error(err))
})
.catch((error) => console.error(error));
构建代码时,出现以下错误:
src/assets-handler.ts:110:5 - error TS2345: Argument of type '{ resource: { 'name': string; 'mimeType': string; }; fields: string; }' is not assignable to parameter of type 'BodyResponseCallback<Schema$File>'.
Object literal may only specify known properties, and 'resource' does not exist in type 'BodyResponseCallback<Schema$File>'.
答案 0 :(得分:0)
我查看了类型定义,create
方法如下:
create(params?: Params$Resource$Files$Create, options?: MethodOptions): AxiosPromise<Schema$File>;
create(params: Params$Resource$Files$Create, options: MethodOptions | BodyResponseCallback<Schema$File>, callback: BodyResponseCallback<Schema$File>): void;
create(params: Params$Resource$Files$Create, callback: BodyResponseCallback<Schema$File>): void;
create(callback: BodyResponseCallback<Schema$File>): void;
再看Params$Resource$Files$Create
会给我这个:
interface Params$Resource$Files$Create {
/**
* Auth client or API Key for the request
*/
auth?: string | OAuth2Client | JWT | Compute | UserRefreshClient;
/**
* Whether to ignore the domain's default visibility settings for the
* created file. Domain administrators can choose to make all uploaded files
* visible to the domain by default; this parameter bypasses that behavior
* for the request. Permissions are still inherited from parent folders.
*/
ignoreDefaultVisibility?: boolean;
/**
* Whether to set the 'keepForever' field in the new head revision. This is
* only applicable to files with binary content in Drive.
*/
keepRevisionForever?: boolean;
/**
* A language hint for OCR processing during image import (ISO 639-1 code).
*/
ocrLanguage?: string;
/**
* Whether the requesting application supports Team Drives.
*/
supportsTeamDrives?: boolean;
/**
* Whether to use the uploaded content as indexable text.
*/
useContentAsIndexableText?: boolean;
/**
* Request body metadata
*/
requestBody?: Schema$File;
/**
* Media metadata
*/
media?: {
/**
* Media mime-type
*/
mediaType?: string;
/**
* Media body contents
*/
body?: any;
};
}
因此,不存在resource
属性,它是requestBody?: Schema$File
。
但是,当我将代码更改为该代码时,它仍然会给我大致相同的错误:
src/assets-handler.ts:110:5 - error TS2345: Argument of type '{ requestBody: any; fields: string; }' is not assignable to parameter of type 'BodyResponseCallback<Schema$File>'.
Object literal may only specify known properties, and 'requestBody' does not exist in type 'BodyResponseCallback<Schema$File>'.
似乎我遇到了create
方法的错误重载。所以我添加了一个空对象参数:
drive.files.create({
requestBody: folderMetadata
}, {})
.then((res) => {
console.log('Created folder Id: ', res.data.id);
})
.catch(err => console.error(err))