T
T
The_XXI2020-06-03 00:14:47
JavaScript
The_XXI, 2020-06-03 00:14:47

How to get JSON data from database?

There is this code:

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");
        });

I call player.loadCharacter()
Console output:
5ed6c09eaa4a1221043239.jpeg

Only output
after console.log(JSON.parse(data)); everything stops working, if you remove it, it will also display 2

Here is the data in 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
}


Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shsv382, 2020-06-03
@shsv382

Most likely, the problem is that the request from the database returns a promise, that is, it is asynchronous, and you are trying to process it as a normal synchronous code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question