![](/img/trans.png)
[英]How do I pass the data from MongoDB to my web api through node.js?
[英]How do I properly route data through my Node API?
我有以下文件:
我的路線 - orders_count 路線所在的位置:
路線/ 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;
然后我將我的訂單計數控制器放在控制器目錄中:
控制器/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 獲取數據。
服務/訂單Service.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 端點?
我一直認為我在控制器中搞砸了一些東西,但已經看了很長時間,似乎無法弄清楚。
任何幫助將不勝感激,如果有什么我可以添加清楚,請不要猶豫,問。
最好。
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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.