【发布时间】:2020-10-19 22:13:19
【问题描述】:
我正在使用 Google Drive API 来提取与特定文件相关的 cmets。但是,当我返回与特定值关联的 cmets 数组时,Typescript 似乎认为我正在从 listCommentsForFile 函数返回 void,而实际上它们将位于 Array
因为googleComments 变量被假定为void,所以我不能像forEach 那样在其上运行数组方法
有人可以指出我如何显式转换为Array,或者让Typescript 知道我将返回Array?我是 TypeScript 的新手,所以我很想知道它是如何工作的。谢谢
//Set my oAuth2Client and fileID vars
const googleComments = await listCommentsForFile(oAuth2Client, fileID.trim());
googleComments.forEach(async comments => {
//Do stuff with the comment
});
/**
* List the comments of all files
* @param {GoogleApis.auth.OAuth2} auth an authorized OAuth2 client.
* @param {string} fileId the id of the file we're pulling comments for
*/
async function listCommentsForFile(auth, fileId) {
const drive = await google.drive({ version: "v3", auth });
let commentsArray: any[];
drive.comments.list(
{
fileId: fileId,
fields: "*",
},
(err, res) => {
if (err) return console.log("The Comments API returned an error: " + err);
if (res && res.data.comments) {
const comments = res.data.comments;
if (comments.length) {
comments.map((comment) => {
commentsArray.push(comment);
});
} else {
console.log("No comments found.");
}
}
return commentsArray;
}
);
}
【问题讨论】:
-
您正在从
list()回调中返回。listCommentsForFile()不返回值,因为它没有return。见stackoverflow.com/questions/14220321/…
标签: arrays typescript casting google-api