如何解决如何通过 Node API 正确路由数据?
我有以下文件:
我的路线 - orders_count 路线所在的位置:
routes/index.js
const express = require('express');
const router = express.Router();
const transactionsController = require('../controllers/transactionsController');
const ordersController = require('../controllers/ordersController');
const ordersCountController = require('../controllers/ordersCountController');
router.get('/transactions',transactionsController);
router.get('/orders',ordersController);
router.get('/orders_count',ordersCountController);
module.exports = router;
然后我将我的订单计数控制器放在控制器目录中:
controllers/ordersCountController.js
const ordersCountService = require('../services/ordersCountService');
const ordersCountController = (req,res) => {
ordersCountService((error,data) => {
if (error) {
return res.send({ error });
}
res.send({ data })
});
};
module.exports = ordersCountController;
然后我的控制器调用我的订单计数服务,该服务从另一个 API 获取数据。
services/ordersService.js
const fetch = require('node-fetch');
// connect to api and make initial call
const ordersCountService = (req,res) => {
const url = ...;
const settings = { method: 'Get'};
fetch(url,settings)
.then(res => {
if (res.ok) {
res.json().then((data) => {
return data;
});
} else {
throw 'Unable to retrieve data';
}
}).catch(error => {
console.log(error);
});
}
module.exports = ordersCountService;
我正在尝试返回 JSON 响应。我最初使用请求进行了设置,但查看 NPM 站点时,它似乎已贬值,因此一直在研究如何使用 node-fetch。
我尝试了“返回数据”和 res.send({data}),但都没有解决问题。
我对此还是个新手,所以我可能遗漏了一些非常明显的内容,但是为什么我没有将 JSON 发送回去,以便它显示在 /api/orders_count 端点上?
我一直认为我在控制器中搞砸了一些东西,但已经看了很长时间,似乎无法弄清楚。
我们将不胜感激,如果有任何我可以添加的内容,请随时提出。
最好的。
解决方法
- 请学习 promises 和 await 语法。生活会更轻松。
- 永远不要抛出字符串。总是喜欢一个真正的错误对象,比如: throw new Error('xxx');这样你总是会得到一个堆栈。它的方式更容易调试。
- 避免回调地狱:http://callbackhell.com/
- 您需要决定是要在控制器中还是在服务中捕获错误。两者都不需要。
- 在控制器中,您以这种方式调用服务:
ordersCountService((error,data) => {
但你是这样声明的:
const ordersCountService = (req,res) => {
不兼容。如果您使用回调样式,它应该如下所示:
const ordersCountService = (callback) => {
...
if (error) return callback(error)
...
callback(null,gooddata);
这里是一个示例,将您的 ordersCountService 函数扁平化以等待语法,它允许您尝试执行“返回数据”:
const fetch = require('node-fetch');
// connect to api and make initial call
const ordersCountService = async (req,res) => {
const url = ...;
const settings = { method: 'Get'};
try {
const res = await fetch(url,settings);
if (!res.ok) throw new Error('Unable to retrieve data');
return await res.json();
} catch(error) {
console.log(error);
}
}
module.exports = ordersCountService;
事实上,我更愿意在控制器中处理错误。那么这足以作为一项服务
const fetch = require('node-fetch');
// connect to api and make initial call
const ordersCountService = async () => {
const url = ...;
const settings = { method: 'Get'};
const res = await fetch(url,settings);
if (!res.ok) throw new Error('Unable to retrieve data');
return await res.json();
}
module.exports = ordersCountService;
那么你可以像这样调用这个函数:
try {
const data = await ordersCountService(req,res);
} catch(err) {
console.log(err);
}
//or
ordersCountService(req,res).then((data) => console.log(data)).catch((err) => console.error(err));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。