我该如何解决这个问题? (node,mysql& async / await)

时间:2017-09-29 21:03:30

标签: mysql node.js express

我是这个领域的新手,我正在使用node.js(使用express),mysql和async / await来测试一些东西,但我找不到修复此错误的方法:

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)

我的数据库模型:

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;

我的帐户型号:

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;

我能做些什么吗?

(PS:对不起,我的语法不好,我还在学习英语)

1 个答案:

答案 0 :(得分:0)

await仅支持标记为async的函数的语法。 Accounts.getByEP不是异步函数,因此尝试await中的某些内容会引发语法错误。写async function (EP, callback)应该解决它。由于async函数已经返回Promise,因此callback可能不是必需的:)

遗憾的是,错误信息有点不清楚。