Remco1337

Can't save vehicle color data in the database.

Recommended Posts

class Vehicle {
    constructor(d) {
        color = [gm.functions.getRandomInt(0, 255), gm.functions.getRandomInt(0, 255), gm.functions.getRandomInt(0, 255)];
        console.log('Creating vehicle!!! | ' + color);
        const pos = JSON.parse(d.coord);
        const vehicle = mp.vehicles.new(d.model, new mp.Vector3(pos.x, pos.y, pos.z),
        {
            heading: pos.rot,
            dimension: pos.dim,
            locked: true,
            engine: false,
        });
        vehicle.guid = d.id;
        vehicle.title = d.title;
        vehicle.fuel = d.fuel;
        vehicle.fuelTank = d.fuelTank;
        vehicle.fuelRate = d.fuelRate;
        vehicle.price = d.price;
        vehicle.ownerId = d.ownerId;
        vehicle.whoCanOpen = JSON.parse(d.whoCanOpen);
        vehicle.factionId = d.factionName;
        vehicle.windowsOpened = [false, false, false, false];
        vehicle.numberPlate = d.numberPlate;

        const primaryColor = JSON.parse(color);
        const secondaryColor = JSON.parse(color);
        vehicle.setColorRGB(primaryColor[0], primaryColor[1], primaryColor[2], secondaryColor[0], secondaryColor[1], secondaryColor[2]);

        gm.mysql.handle.query('INSERT INTO `vehicles` SET vehicleId = 5, vehicleModel = ?, vehiclePosX = ?, vehiclePosY = ?, vehiclePosZ = ?, vehicleRotating = ?, vehicleDimension = ?, vehicleTitle = ?, vehicleFuel = ?, vehicleFuelTank = ?, vehicleFuelRate = ?, vehiclePrice = ?, vehicleOwner = ?, vehicleWhoCanOpen = 1, vehicleFactionId = 0, vehicleNumberPlate = "Noob", vehiclePrimaryColor = ?, vehicleSecondaryColor = ?',
            [d.model, pos.x, pos.y, pos.z, pos.rot, pos.dim, vehicle.title, vehicle.fuel, vehicle.fuelTank, vehicle.fuelRate, vehicle.price, vehicle.ownerId, vehicle.whoCanOpen, `${JSON.stringify(color)}`, `${JSON.stringify(color)}`], function (err, res) {
            if (!err) {
                console.log(`VEHICLE HAS BEEN SAVED`);
            } else {
                console.log("\x1b[31m[ERROR] " + err)
            }
        });

        return vehicle;
    }
}
module.exports = Vehicle;

This is the output it gives; 

Creating vehicle!!! | 135,193,84
undefined:1: SyntaxError: Unexpected token , in JSON at position 3

So I am assuming it's because of the first ',' but I have been struggeling this for hours and can't seem to find the solution for this problem :( Also tried without the JSON.stringify didn't work also.

Share this post


Link to post
Share on other sites

Why parse here?

const primaryColor = JSON.parse(color);
const secondaryColor = JSON.parse(color);

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.