@The_XXI

Как получить данные в формате JSON из БД?

Есть вот такой код:
player.applyCharacter = function() {
        this.setCustomization(
            !(this.customCharacter.Gender == 0),

            this.customCharacter.Parents.Mother,
            this.customCharacter.Parents.Father,
            0,

            this.customCharacter.Parents.Mother,
            this.customCharacter.Parents.Father,
            0,

            this.customCharacter.Parents.Similarity,
            this.customCharacter.Parents.SkinSimilarity,
            0.0,

            this.customCharacter.EyeColor,
            this.customCharacter.Hair.Color,
            this.customCharacter.Hair.HighlightColor,

            this.customCharacter.Features
        );

        this.setClothes(2, this.customCharacter.Hair.Hair, 0, 2);
        for (let i = 0; i < 10; i++) this.setHeadOverlay(i, [this.customCharacter.Appearance[i].Value, this.customCharacter.Appearance[i].Opacity, this.colorForOverlayIdx(i), 0]);
        console.log("successfully");
    };

    player.loadCharacter = function() {
        console.log("loader");
        mysql.Handle.query('SELECT `person_character` FROM `persons` WHERE `id` = ?', [player.pers_id], function(err, data){
            console.log("1");
            if(err) console.log(err);
            console.log(JSON.parse(data));
            console.log("2");
            this.customCharacter = JSON.parse(data);
            console.log("3");
            this.applyCharacter();
            console.log("successfully 2");
        });

Вызываю player.loadCharacter()
Вывод в консоли:
5ed6c09eaa4a1221043239.jpeg

Выводится только
после console.log(JSON.parse(data)); все перестает работать, если убрать его, то выведется еще и 2

Вот данные в person_character:
{
    "Gender": 1,
    "Parents": {
        "Father": 0,
        "Mother": 38,
        "Similarity": 0.54,
        "SkinSimilarity": 0.27
    },
    "Features": [
        -0.37,
        -0.92,
        -0.5,
        -0.46,
        0.31,
        -0.47,
        0.86,
        -0.84,
        0.78,
        -0.25,
        -0.49,
        -0.91,
        0.07,
        -0.19,
        -0.67,
        0.75,
        0.25,
        -0.36,
        -0.11,
        0.17
    ],
    "Appearance": [
        {
            "Value": 4,
            "Opacity": 0.49
        },
        {
            "Value": 22,
            "Opacity": 0.61
        },
        {
            "Value": 29,
            "Opacity": 0.93
        },
        {
            "Value": 4,
            "Opacity": 0.67
        },
        {
            "Value": 22,
            "Opacity": 0.51
        },
        {
            "Value": 10,
            "Opacity": 0.26
        },
        {
            "Value": 4,
            "Opacity": 0.25
        },
        {
            "Value": 6,
            "Opacity": 0.6
        },
        {
            "Value": 3,
            "Opacity": 0.09
        },
        {
            "Value": 6,
            "Opacity": 0.9400000000000001
        },
        {
            "Value": 5,
            "Opacity": 0.62
        }
    ],
    "Hair": {
        "Hair": 13,
        "Color": 56,
        "HighlightColor": 27
    },
    "EyebrowColor": 53,
    "BeardColor": 20,
    "EyeColor": 3,
    "BlushColor": 6,
    "LipstickColor": 26,
    "ChestHairColor": 14
}


Почему так происходит и как это исправить?
  • Вопрос задан
  • 134 просмотра
Пригласить эксперта
Ответы на вопрос 1
@shsv382
Скорее всего проблема в том, что запрос из БД возвращает промис, то есть он асинхронный, а ты пытаешься его обработать как обычный синхронный код.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы