M
M
Magnus Keef2018-12-13 00:08:43
JavaScript
Magnus Keef, 2018-12-13 00:08:43

How to insert a variable here?

request = new Request("SELECT * FROM [driving license] WHERE name_driver='Олег'", function (err, rowCount, rows) {

How to make a login variable in this line instead of Oleg???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ihor Bratukh, 2018-12-13
@SecurityYourFingers

var text = "SELECT * FROM [driving license] WHERE name_driver={{user.name}}";

templater(text, {
    user: {
        name: 'Олег'
    }
});

//> "SELECT * FROM [driving license] WHERE name_driver=Олег"


var text = "SELECT * FROM [driving license] WHERE name_driver={{name}}";

templater(text, {
    name: 'Олег'
});

//> "SELECT * FROM [driving license] WHERE name_driver=Олег"

/**
 * Обработка тегов, шаблонизатор
 * @param {String} Входные данные
 * @param {Object} Объект {тег - значение}
 * @returns {String} Выходные, обработаные данные
 **/
function templater(input, data) {
    if (!['string', 'object'].includes(typeof input)) {
        return input;
    }
    for (var key in data) {
        if (Object.prototype.hasOwnProperty.call(data, key)) {
            if (typeof data[key] === 'object' && data[key] !== null && !Array.isArray(data[key])) {
                var nested = {};
                for (var name in data[key]) {
                    nested[key + '.' + name] = data[key][name];
                }
                input = templater(input, nested);
            } else {
                input = input.replace(new RegExp('\{\{' + key + '\}\}', 'g'), data[key]);
            }
        }
    }
    return input;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question