💻 آخرین فرصت یادگیری برنامه‌نویسی با آفر ویژه قبل از افزایش قیمت در ۱۵ آذر ماه (🎁 به همراه یک هدیه ارزشمند )
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ مصطفی قدیم پور
در مورد نوشتن query
جامعه نود جی اس ایجاد شده در ۱۷ خرداد ۱۳۹۹

باسلام و عرض وقت بخیر خدمت استاد عزیز 
علت این ارور چی میتونه باشه؟

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
5000
App is working on port 5000
Connected!
(node:5868) UnhandledPromiseRejectionWarning: TypeError: db.query is not a function
    at Object.exports.totalUsers (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\app\models\statistics.js:4:31)
    at Object.eval [as main] (eval at createFunctionContext (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:262:23), <anonymous>:9:249)
    at main (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\node_modules\handlebars\dist\cjs\handlebars\runtime.js:208:32)
    at ret (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\node_modules\handlebars\dist\cjs\handlebars\runtime.js:212:12)
    at ret (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\node_modules\handlebars\dist\cjs\handlebars\compiler\compiler.js:519:21)
    at ExpressHandlebars._renderTemplate (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\node_modules\express-handlebars\lib\express-handlebars.js:250:9)
    at ExpressHandlebars.<anonymous> (C:\Users\MostafaG\Desktop\JS 7L\07-Make website\Project\node_modules\express-handlebars\lib\express-handlebars.js:173:15)
(node:5868) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5868) [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.

 

1)

const db = require("../../database/mysql")
exports.totalUsers = async () => {
    const [result] = await db.query("SELECT COUNT(id) as totalUsers FROM users");
    return result[0].totalUsers;
}

2)I don't use .env because i had error about password but connection is ok
 

const mysql = require("mysql2");
const connection = mysql.createConnection({
    /*host: process.env.MYSQL_HOST,
    port: process.env.MYSQL_PORT,
    user: process.env.MYSQL_PORT,
    database: process.env.MYSQL_DATABASE,
    password: process.env.MYSQL_PASSWORD*/
    host: "localhost",
    port: 3306,
    user: "root",
    database: "weblog"
});
connection.connect((err) => {
    if (err) throw err;
    console.log("Connected!");
  });
module.exports = connection.promise;


 

سلام باید promise رو به تابع فراخوانی کنید.

کیوان علی محمدی ۱۷ خرداد ۱۳۹۹، ۱۰:۴۵