【问题标题】:Unable to add sails-mysql as non-default datastore无法将sails-mysql 添加为非默认数据存储
【发布时间】:2021-05-20 00:16:48
【问题描述】:

我被困了好几天,试图将 mysql 作为非默认数据存储添加到我的sails 项目中。如果有人知道解决方案。请

似乎 config/datastores.js 中的附加数据存储未注册,或者显示的错误不正确。

“帆”:“^1.4.0”, “帆-mysql”:“^1.0.1”

我不断收到此错误:

错误:钩子 (orm) 加载失败!错误:无法拆除 ORM 钩子。错误详细信息:错误:数据存储标识无效。没有数据 商店以该身份存在。

config/datastores.js

module.exports.datastores = {
default: {
    adapter: 'sails-disk',
},

mysqlDB: {
    adapter: 'sails-mysql',
    url: `mysql://${process.env.TC_DB_USERNAME}:${process.env.TC_DB_PASSWORD}@${process.env.TC_DB_HOST}:3306/Auth`,
},
};

api/models/Account.js

module.exports = {
 datastore: 'mysqlDB',
 schema: false,
};

作为一种解决方法,我使用标准的“mysql”包在助手内部进行连接。这工作得很好,但是资源很重,我很想获得标准的sails方法(Account.sendNativeQuery())来让模型工作。 如果我不能让它工作,也许有人可以告诉我如何使这个连接全局化,所以我只需要连接一次,而不是每次调用助手?

helpers/new-account.js

    var mysql = require('mysql');
var connection = mysql.createConnection({
    host: process.env.TC_DB_HOST,
    user: process.env.TC_DB_USERNAME,
    password: process.env.TC_DB_PASSWORD,
    database: 'Auth',
});

connection.connect();

module.exports = {
    friendlyName: 'Creates new account',

    description: 'returns the id of the new usr or throws error',

    inputs: {
        username: {
            description: 'The new account name.',
            type: 'string',
            required: true,
            minLength: 4,
            maxLength: 64,
        },
        salt: {
            required: true,
            type: 'ref',
            description: 'generated salt.',
        },
        verifier: {
            required: true,
            type: 'ref',
            description: 'generated verifier.',
        },
        email: {
            required: true,
            type: 'string',
            isEmail: true,
            description: 'Email linked to account.',
        },
    },

    exits: {
        success: {
            description: 'All done.',
        },
    },

    fn: async function ({ username, salt, verifier, email }, exits) {

        connection.query(
            'INSERT INTO account SET ?',
            { username, salt, verifier, email },
            function (err, result) {
                if (err) {
                    throw new Error(`MYSQL ERROR: ${err}`);
                } else {
                    console.log(`new user with id ${result.insertId} created.`);
                    exits.success(result.insertId);
                }
            }
        );

        connection.end();
    },
    };

谢谢

【问题讨论】:

    标签: mysql sails.js


    【解决方案1】:

    尝试要求 sails-mysql 包,而不仅仅是包含字符串化的名称,例如:

    module.exports.datastores = {
        default: {
            adapter: 'sails-disk',
        },
        mysqlDB: {
            adapter: require('sails-mysql'),
            url: `mysql://${process.env.TC_DB_USERNAME}:${process.env.TC_DB_PASSWORD}@${process.env.TC_DB_HOST}:3306/Auth`,
        },
    };
    

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2015-04-06
      • 2020-08-15
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      相关资源
      最近更新 更多