I got two files one between1.js and the second one is import.js. In between1.js I got few functions and one of the functions I moved to import.js. The exercise I got is to use the module and require function. The problem I still have how to use require in between1.js. See code below
Between1.js code : https://codeshare.io/alxMvY
import.js : https://codeshare.io/244yg4
I am beginners on js and node. I got function in import.js and on the bottom of code is module.exports that mean exporting module and in the second file between1.js, I try to import the function by first : let utils = require("./import.js"); And on the bottom trying to get the function but the problem is I don't know really how. I was trying like I paste before in code but it dosesn't work
What I want is to someone help me explain how to require and module work.
import.js code :
/**
* Module.
*
* Matteus Urbaniak
*/
"use strict";
function teacherAsTable(res) {
let str;
str = "+-----------+---------------------+-----------+----------+--------------+\n";
str += "| Akronym | Namn | Avdelning | Lön | Kompetens |\n";
str += "|-----------|---------------------|-----------|----------|--------------|\n";
for (const row of res) {
str += "| ";
str += row.akronym.padEnd(10);
str += "| ";
str += (row.fornamn + " " + row.efternamn).padEnd(20);
str += "| ";
str += row.avdelning.padEnd(10);
str += "| ";
str += row.lon.toString().padStart(9);
str += "| ";
str += row.kompetens.toString().padEnd(12);
str += " |\n";
}
str += "+-----------+---------------------+-----------+----------+--------------|\n";
return str;
}
module.exports = {
"teacherAsTable": teacherAsTable
};
between1.js
/**
Arbete gjordes av Matteus Urbaniak
*/
"use strict";
/**
Jag använder programmeringsstilen med async/await
för att hantera asynkrona anrop. För det syftet väljer
jag paketet promise-mysql som är en wrapper kring paketet mysql.
*/
const mysql = require("promise-mysql");
// Inloggningsdetaljer till databasen.
const config = require("./config.json");
let utils = require("./import.js");
// Läsa från komand linje
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
/**
Main function
*/
(async function() {
const db = await mysql.createConnection(config);
let str;
rl.question("What do you want search for? (min); ", async (min) => {
rl.question("What do you want search for? (max); ", async (max) => {
str = await searchBetween(db, min, max);
console.info(str);
rl.close();
db.end();
});
});
})();
// Ge en rapport med larare details och formaterad text.
async function searchBetween(db, min, max) {
let sql;
let res;
let str;
let minimum = `${min}`;
let maximum = `${max}`;
console.info(`Searching for: ${minimum} - ${maximum}`);
sql = `
SELECT
akronym,
fornamn,
efternamn,
avdelning,
lon,
kompetens
FROM larare
WHERE
(kompetens BETWEEN ? AND ?)
OR
lon BETWEEN ? AND ?
ORDER BY akronym;
`;
res = await db.query(sql, [minimum, maximum, minimum, maximum]);
str = teacherAsTable(res);
return str;
}
// HERE SHOULD BE teacherAsTable
let res;
res = utils.teacherAsTable();
console.log(res);
尝试在Between1.js中执行以下操作:
module.exports = {teacherAsTable};
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.