- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Node JS 还很陌生,所以我有点挣扎。
我正在尝试使用 Google Drive 的 API 从我的 Node Js 代码读取文件。
我有以下代码
router.get('/', function (req, res, next) {
var pageToken = null;
// Using the NPM module 'async'
async.doWhilst(function (callback) {
drive.files.list({
q: "mimeType='image/jpeg'",
fields: 'nextPageToken, files(id, name)',
spaces: 'drive',
pageToken: pageToken
}, function (err, res) {
if (err) {
// Handle error
console.error(err);
callback(err)
} else {
res.files.forEach(function (file) {
console.log('Found file: ', file.name, file.id);
});
pageToken = res.nextPageToken;
callback();
}
});
}, function () {
return !!pageToken;
}, function (err) {
if (err) {
// Handle error
console.error(err);
} else {
// All pages fetched
}
})
res.render('index', { title: 'Express' });
});
当我发送 get 请求时,上面的代码给了我以下错误
(node:13884) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at E:\nodejs\newcelebapi\routes\index.js:49:17
at E:\nodejs\newcelebapi\node_modules\googleapis-common\build\src\apirequest.js:43:53
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:13884) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13884) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
问题出在以下行
res.files.forEach(function (file) {
我已经尽了一切努力,但放弃了理解这个问题。
你能帮我解决这个问题吗?
谢谢!
最佳答案
每this example in the doc ,您想要使用:
res.data.files.forEach(...)
不是:
res.files.forEach(...)
而且,看起来一旦解决了这个问题,您就会遇到另一个问题,因为您在错误的位置调用了 res.render()
。而且,当您解决此问题后,您将遇到在 Google 回调中重新定义 res
的问题,这将隐藏 res.render 所需的更高级别的
.res
()
我强烈建议您不要在此处使用async
库。这里似乎不需要它,而且它只会使事情变得复杂。而且,如果您确实需要协调异步操作的帮助,Promise 是实现这一目标的现代方法。
您没有显示您尝试对结果文件执行什么操作(除了记录它们),但这里有一个简单的实现可以做到这一点:
router.get('/', function (req, res, next) {
var pageToken = null;
drive.files.list({
q: "mimeType='image/jpeg'",
fields: 'nextPageToken, files(id, name)',
spaces: 'drive',
pageToken: pageToken
}).then(response => {
let files = response.data.files;
files.forEach(file => console.log('Found file: ', file.name, file.id))
res.render('index', { title: 'Express' });
}).catch(err => {
console.log(err);
res.sendStatus(500);
});
});
请注意,我将 drive.files.list()
中的参数命名为 response
而不是 res
,以便我可以访问来自 router.get()
回调的 res
和来自 drive.files.list()
回调的 response
。您为它们指定了相同的名称 res
,这意味着您无法访问同名的 router.get()
参数。
关于javascript - NodeJS - 如何处理 UnhandledPromiseRejectionWarning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60250759/
我有 await 语句,它们可能会引发错误,因此我在 try/catch 内执行它们。但是 try/catch 没有捕获它们,我收到警告: (node:4496) UnhandledPromiseRe
最近,我在ionic中创建了一个应用程序,我想在ios中对其进行测试。所以我运行命令ionic cordova emulate ios。但是,这给了我以下错误。 (节点:4200)UnhandledP
我正在尝试使用 node.js discord 模块制作一个机器人,当在 discord 中输入命令时,它会向用户发送私有(private)消息。该命令应该像这样使用 !rp 然后邮件将发送给收件人
我正在尝试调用一个异步方法。异步方法是: async connect() { this.pool = await mysql.createPool(this.conf);
我按议程包编写了一些代码,但在运行我的项目时遇到了 UnhandledPromiseRejectionWarning。 我的代码在这里: agenda.define('transferDBField'
我有一个代码: function () { return new Promise((resolve, reject) => { try { fetch(URL, { metho
以下是我在 graphql(apollo-server)服务器中的 onDisconnect 函数代码(但不是特定于 graphql 的)。它包含一个 postgres 事务,通过 DB 适配器使用。
我是 Javascript Promise 的新手。我正在尝试以基于 Promises 的方式执行 MySQL 查询,我已经挠头半个小时了,但我仍然找不到这段代码中的问题。我正在使用this arti
尝试从 localhost:3000/todos/在 mongodb 中创建数据后获取数据,我在 Node 中收到错误 [UnhandledPromiseRejectionWarning: 错误 [E
我收到以下错误: Houston we got an err at getGames in index.js (node:72197) UnhandledPromiseRejectionWarning
所以我正在使用 Express.JS 和 MongoDB 制作一个 Web 应用程序,当我尝试在本地运行它时,它可以工作,但是当我用我的 aws Ubuntu 20.04 机器运行它时,它给了我以下错
我在调用其他发出 axios 请求的函数的函数中处理 UnhandledPromiseRejectionWarning 时遇到问题。 const getCode = async () => { c
我对 Node JS 还很陌生,所以我有点挣扎。 我正在尝试使用 Google Drive 的 API 从我的 Node Js 代码读取文件。 我有以下代码 router.get('/', funct
我正在使用 Angular/Firebase 在 Ionic 中构建一个应用程序。 我想在我的 Android 设备上测试该应用程序,该设备以前运行良好。但是,最近我无法运行它,因为出现错误。 $ i
这个问题在这里已经有了答案: How is PromiseRejectionEvent triggered? (2 个答案) 关闭 3 年前。 如果我在拒绝 promise 后清除计时器 - 无论我
我的目标是使用 Node.js 从网站上抓取一些数据. 我已经设法仅使用request 包来抓取数据,但是我要抓取的站点具有动态内容,而request 只能抓取此动态数据。 所以我做了一些研究,发现要
我玩过 Mocha 测试。我注意到我的函数在少数地方引发了 “UnhandledPromiseRejectionWarning” 警告。它使脚本无法通过检查。 我无法在互联网上找到有用的示例来教授/展
最近,我在测试代码时经常遇到此错误。刚才我在尝试执行包含错误的同步代码时再次收到此错误: (async function() { const characterProfile = await Mo
我知道涉及 SO 上的 UnhandledPromiseRejectionWarning 问题的现有答案,但找到的答案似乎都与我的情况不充分匹配。基本上,我以为我知道如何处理 promise 。但这个
我当前的nodejs版本是:8.9.1 我正在尝试执行我的代码,但总是遇到此错误,尝试修复它,但总是出现相同的错误: (node:38) UnhandledPromiseRejectionWarnin
我是一名优秀的程序员,十分优秀!