i trying to make an apps who can read a JSON with NodeJs.
What i did get good result ( perhaps slow to execute. ) I would like an expert view who can take a look on my code, cause i feel lost and confuse...
Actually: killboard.js using bent for JSON
const bent = require('bent');
const getJSON = bent('json');
const config = require('../config.json');
function showKill(kill)
{
console.log(kill.Killer.Name + " est un meurtrier !");
}
function getKill(killsList) {
return new Promise((resolve, reject) => {
killsList.some(function(kill, index) {
console.log("One kill ...");
showKill(kill);
});
});
}
module.exports = {
exec: async() =>
{
let killsList = await getJSON('https://gameinfo.albiononline.com/api/gameinfo/events?limit=51&offset=0');
await getKill(killsList);
}
}
And my APP:
const KillBoar = require('./module/killboard');
KillBoar.exec();
It works fine....
But how can i integrate: showKill and getKill canonically inside the "module.exports". I mean something like that:
module.exports = {
exec: async() =>
{
let killsList = await getJSON('https://gameinfo.albiononline.com/api/gameinfo/events?limit=51&offset=0');
await getKill(killsList);
},
getKill(killsList) {
return new Promise((resolve, reject) => {
killsList.some(function(kill, index) {
console.log("One kill ...");
showKill(kill);
});
});
},
showKill(kill)
{
console.log(kill.Killer.Name + " est un meurtrier !");
}
}
Cause if i do, i have this error:
(node:7392) UnhandledPromiseRejectionWarning: ReferenceError: getKill is not defined
at Object.exec (C:\Users\Baptiste\Desktop\BotDiscord\KillBoard\module\killboard.js:26:9)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:7392) 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(). (rejection id: 1)
(node:7392) [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.
Thank you to take time to answer !
You changed them from function definitions (that are called as plain functions) to methods of the module.exports
object, so you now need to call them as methods on that:
module.exports.getKill(killsList);
…
module.exports.showKill(kill);
Alternatively, you might get away with this.getKill(…)
/ this.showKill(…)
, see here for details on the difference.
A third alternative would be to keep them as functions, and just add them to the export. Then you could call them either way:
function showKill(kill) { … }
module.exports = {
async execc() { … },
showKill,
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.