Auth0创建用户脚本,给我一个错误“ mysql不是函数”

时间:2019-11-22 05:55:02

标签: mysql database auth0

这是我的代码,用于创建在Auth0中使用自定义数据库的用户脚本

function create(user, callback) {
  const mysql = require('mysql');
  const bcrypt = require('bcrypt');

  const connection = mysql({
    host: configuration.host,
    user: configuration.user,
    password: configuration.password,
    database: configuration.database
  });

  connection.connect();

  const query = 'INSERT INTO users SET ?';

  bcrypt.hash(user.password, 10, function(err, hash) {
    if (err) return callback(err);

    const insert = {
      password: hash,
      email: user.email
    };

    connection.query(query, insert, function(err, results) {
      if (err) return callback(err);
      if (results.length === 0) return callback();
      callback(null);
    });
  });
}

The error

1 个答案:

答案 0 :(得分:0)

似乎没有调用createConnection。所以基本上应该是这样的:

function create(user, callback) {
    const mysql = require('mysql');
    const bcrypt = require('bcrypt');

    var connection = mysql.createConnection({
      host: configuration.host,
      user: configuration.user,
      password: configuration.password,
      database: configuration.database
    });

    connection.connect(function(err) {
        if (err) return callback(err);

        var query = 'INSERT INTO users SET ?';
        bcrypt.hash(user.password, 10, function(err, hash) {
            if (err) return callback(err);

            var values = {
              password: hash,
              email: user.email
            };

            connection.query(query, values, function (err, result) {
                if (err) return callback(err);
                if (result.length === 0) return callback();
                callback(null);
            });
        });
    });
}