Jump to content
RAGE Multiplayer Community

Passing variables from database into CEF


DenisCatalin
 Share

Recommended Posts

Posted (edited)

Hello,

I know that this issue exists already here on the forum and I've seen few of them. Nothing seems to work for me. I want to create a dealership and basically I try to pass some data from the database (MySQL) to CEF.

 

Server-side:

mp.events.add("loadDealershipCoupes", (player) => {
    gm.mysql.handle.query("SELECT * FROM `dealership_vehicles` WHERE `Type` = 'Coupes' ORDER BY `Price`", [], ((err, res) => {
        if(err) console.log(err);
        dealership_vehicles = [];
        for(let i = 0; i < res.length; i++) {
            dealership_vehicles[i] = new Object();
            dealership_vehicles[i].ID = res[i].ID; 
            dealership_vehicles[i].Model = res[i].Model;
            dealership_vehicles[i].Price = res[i].Price;
            dealership_vehicles[i].Stock = res[i].Stock;
            dealership_vehicles[i].Type = res[i].Type;
            dealership_vehicles[i].Speed = res[i].Speed;
            dealership_vehicles[i].Acceleration = res[i].Acceleration;
            dealership_vehicles[i].Brakes = res[i].Brakes;
            dealership_vehicles[i].Handling = res[i].Handling;
            dealership_vehicles[i].Seats = res[i].Seats;
            dealership_vehicles[i].Gears = res[i].Gears;
            dealership_vehicles[i].DisplayName = res[i].DisplayName;
        }
        player.call('putItemsInList', dealership_vehicles[0].DisplayName, dealership_vehicles[0].Price);
        console.log(dealership_vehicles[0].DisplayName, dealership_vehicles[0].Price);
    }));
});

As you can see, I also have a console.log for the first item. All looks good in console, it gives me the name of the vehicle and the price of it.

 

client-side:

mp.events.add("putItemsInList", (name, price) => {
    browser.execute(`UI.update_list(${name}, ${price});`);
});

 

main.js:

class UI {
    static update_list(vehicleName, vehiclePrice) {
        list.innerHTML += `
        <div class="item">
            <div class="icon">
                <i class="fas fa-caret-right"></i>
            </div>
            <div class="details">
                <h3 class="name-vehicle">${vehicleName}</h3>
                <p class="price-vehicle">$${vehiclePrice}</p>
            </div>
        </div>
        `;
    }
}

 

So... do you guys have any idea of how I can solve this issue?

Also, I won't need only the first vehicle of the 'Coupes' type, but I wanted to test it first like that.

Everything I would do, it gives me "undefined".

Edited by DenisCatalin
Link to comment
Share on other sites

Replace 

player.call('putItemsInList', dealership_vehicles[0].DisplayName, dealership_vehicles[0].Price);

with

player.call('putItemsInList', [dealership_vehicles[0].DisplayName, dealership_vehicles[0].Price]);

 

Link to comment
Share on other sites

Also I don't really understand why you're re-creating exists object with the same properties if you can just do something like this:

mp.events.add("loadDealershipCoupes", (player) => {
    gm.mysql.handle.query("SELECT * FROM `dealership_vehicles` WHERE `Type` = 'Coupes' ORDER BY `Price`", [], ((err, res) => {
        if(err) {
          console.log(err);
          return; // <---- here is you forgot return, it's needed because if error occurs you don't receives data from db
        }
        player.call('putItemsInList', [res[0].DisplayName, res[0].Price]);
        console.log(res[0].DisplayName, res[0].Price);
    }));
});

 

Link to comment
Share on other sites

Well, thanks for your reply. The reason I re-create the object is that in other functions I want to manipulate some variables there so I have to store them somehow. 

Anyway, I replaced my code to yours and it's still not working. 

player.call('putItemsInList', [res[0].DisplayName, res[0].Price]);

Is not working at all, and:

player.call('putItemsInList', res[0].DisplayName, res[0].Price);

is still undefined (https://imgur.com/a/NAH489v)

 

Link to comment
Share on other sites

require("../mysql.js");

let dealership_vehicles = []; 

mp.events.add("loadDealershipCoupes", (player) => {
    gm.mysql.handle.query("SELECT * FROM `dealership_vehicles` WHERE `Type` = 'Coupes' ORDER BY `Price`", [], ((err, res) => {
        if(err) { console.log(err); return; }
        dealership_vehicles = [];
        for(let i = 0; i < res.length; i++) {
            dealership_vehicles[i] = new Object();
            dealership_vehicles[i].ID = res[i].ID; 
            dealership_vehicles[i].Model = res[i].Model;
            dealership_vehicles[i].Price = res[i].Price;
            dealership_vehicles[i].Stock = res[i].Stock;
            dealership_vehicles[i].Type = res[i].Type;
            dealership_vehicles[i].Speed = res[i].Speed;
            dealership_vehicles[i].Acceleration = res[i].Acceleration;
            dealership_vehicles[i].Brakes = res[i].Brakes;
            dealership_vehicles[i].Handling = res[i].Handling;
            dealership_vehicles[i].Seats = res[i].Seats;
            dealership_vehicles[i].Gears = res[i].Gears;
            dealership_vehicles[i].DisplayName = res[i].DisplayName;
        }
        player.call('putItemsInList', dealership_vehicles[0].DisplayName, dealership_vehicles[0].Price);
        console.log(dealership_vehicles[0].DisplayName, dealership_vehicles[0].Price);
    }));
});
                                      
                                      //As you can see, there is an empty array at the top.

 

Link to comment
Share on other sites

I solved my issue, thanks anyway!

 

I should have put quotations here:

mp.events.add("putItemsInList", (name, price) => {
    browser.execute(`UI.update_list("${name}", ${price});`);
});

I appreciate the help @Kazein. This topic can be closed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...