我创建了一个 node.js 网络应用程序。目前我正在为数据库使用 MSSQL。我创建了函数 (sql.js) 来执行相应的 SQL 函数。我还创建了一个异步函数处理程序 (controllers.js),因此我可以在我的路由器 (express) 中使用 async 和 await 关键字。路由器使用 await 关键字调用它们。我在调用这些函数时没有遇到任何错误,事实上一切都返回数据并正常运行,但在我的文本编辑器 (VSCode) 中,我收到以下错误
[eslint] Parsing error: Unexpected token =>
在进一步调查中,我注意到在我的控制台中出现了未处理的 promise 错误。这让我觉得我错误地定义了我的 Controller
internal/process/warning.js:18 (node:16328) Warning: a promise was created in a handler at D:\Programs\ColdFusion2016\cfusion\wwwroot\vue_starter\node_modules\express\lib\router\index.js:335:12 but was not returned from it, see http:// goo.gl/ rRqMUw
at Function.Promise.attempt.Promise.try (D:\Programs\ColdFusion2016\cfusion\wwwroot\vue_starter\node_modules\bluebird\js\release\method.js:29:9)
.有没有人看到 Controller 有问题?
路由器.js
const express = require('express');
const router = express.Router();
const {
asyncGet,
asyncPost
} = require('../services/controllers')(router);
const sql = require('../services/sql');
const request = require('../services/request');
/// Get Users
asyncGet('/controller/getUsers', async(req, res, next) => {
console.log('get: /controller/getUsers');
const users = await sql.getUsers();
res.status(200).json(users);
});
/// Get Products
asyncGet('/controller/getProducts', async(req, res, next) => {
console.log('get: /controller/getProducts');
const products = await sql.getProducts();
res.status(200).json(products);
});
module.exports = router;
Controller .js
module.exports = (router) => {
const asyncGet = (route, handler) => {
return router.get(route, (req, res, next) => {
handler(req, res, next)
.catch(error => {
next(error);
});
});
};
const asyncPost = (route, handler) => {
return router.post(route, (req, res, next) => {
handler(req, res, next)
.catch(error => {
next(error);
});
});
};
return { asyncGet, asyncPost };
};
最佳答案
为了让ESLint支持async..await
,它支持should be enabled在 ESLint 配置中:
{
"parserOptions": {
"ecmaVersion": 2017
}
}
关于javascript - JS - 异步函数的解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51102470/