简体   繁体   中英

How do i fix this issue? (node, mysql & async/await)

I'm new to this area and i'm testing some things with node.js (with express), mysql and async/await but i cannot find a way to fix this error:

const user = await con.query(sql, [EP.email, EP.password]);
                   ^^^ SyntaxError: Unexpected identifier at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/Sern/Documents/test/server/lib/routes/login.js:4:16)

My Database Model:

var database = {};

database.con = async function() {

  const  mysql = require('mysql2/promise');

  const connection = await mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "test",
    multipleStatements: true   });   }

module.exports = database;

My Accounts Model:

const database = require('../utils/database')

const con = database.con;

const Accounts = {};

      Accounts.getByEP = function (EP, callback) {

          var sql = "SELECT * FROM accounts WHERE email = ? and password = ?;";

          const user = await con.query(sql, [EP.email, EP.password]);

          callback(user);

      };

module.exports = Accounts;

Is there anything that i could do?

(PS: Sorry for my bad grammar i'm still learning english)

await is only supported syntax in functions that are tagged async . Accounts.getByEP is not an async function, so trying to await something in it will throw a syntax error. Writing async function (EP, callback) should fix it. Since the async function would already return a Promise, maybe the callback is not necessary :)

The error message is a bit unclear unfortunately.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM