Search the Community
Showing results for tags 'resolved'.
-
EDIT: Solved my own issue. I forgot to add options["headers"] and add options["headers"]["Content-Type"] = "application/json". Hi everybody, I am currently writing an API in NodeJS that reads from and writes to a MySQL database. This is used to retrieve and update players' accounts, vehicles etcetera on my server. To make calls to the API I am using the fetchRemote function and handle the data that comes back through a callback function. This works really well and I already have GET and POST requests that work perfectly. I am now at a point where I'm trying to use the HTTP 'PATCH' method to update a record via the API ( e.g. update a vehicle based on the current element's colors, model, etc.), but am running into an issue where using "PATCH" as the HTTP method does not send along any data (such as the postData variable) On the NodeJS side, I keep getting the message that no data was received. When I make this same call with a program like Postman, it works just fine. Is there another way to pass the data for a patch request that is perhaps not documented, and is the patch method even supported by fetchRemote? Lua code on the MTA server side (domains are whitelisted and the resource has the fetchRemote ACL permission) -- The Data to send data = { model = 411, name = 'Test Vehicle', color = '255,255,255,255,255,255,255,255,255,255,255,255', variant = '255,255' } -- fetchRemote options options = { method = "PATCH", postData = string.sub(toJSON(data), 2, -2), -- string.sub to remove [] brackets } -- fetchRemote function calling a vehicle endpoint fetchRemote( "http://localhost:3000/vehicles/3", options, -- URL updates vehicle with ID 3. function(responseData, responseInfo) -- Callback to process response outputDebugString('Status code: ' .. responseInfo['statusCode']) -- Output received statuscode. end ) For those familiar with NodeJS and the Express framework, this is the code on the NodeJS side: router.patch("/:id", bodyParser.json(), (req, res) => { const v = req.body const { id } = req.params const connection = mysql.createConnection( config.db ) console.log(`Attempting to update vehicle with ID: ${id}`) console.log(`Received data:`) console.log(v) if( v.model && v.name && v.variant && v.color && v.upgrades && id) { connection.query(`UPDATE mta.vehicles SET name = '${v.name}', model = '${v.model}', variant = '${v.variant}}', upgrades = '${v.upgrades}', spawn_type = '${v.spawn_type}', spawn_data = '${v.spawn_data}', plate = '${v.plate}' WHERE (\`id\` = '${id}'); `, (error, results, fields) => { if (error) throw error; if( results.changedRows == 1) { console.log("Updated vehicle") res.status(200).send() } else { console.log("Affected rows returned 0") res.status(500).send() } }); } else { console.log("Refusing to update: Missing essential data") res.status(500).send() } });
-
I am new in MTA scripting. Created first resource. It's meta: <meta> <script src="vehicle.lua" type="server"/> <script src="myClient.lua" type="client"/> </meta> Vehicle.lua is working, it's content: function createVehicleForPlayer(thePlayer, command, vehicleModel) local x,y,z = getElementPosition(thePlayer) -- get the position of the player x = x + 5 -- add 5 units to the x position local createdVehicle = createVehicle(tonumber(vehicleModel),x,y,z) -- check if the return value was ''false'' if (createdVehicle == false) then -- if so, output a message to the chatbox, but only to this player. outputChatBox("Failed to create vehicle.",thePlayer) end end addCommandHandler("createvehicle", createVehicleForPlayer) myClient.lua is not working at all, content: function escapeMe ( commandName ) local x, y, z = getElementPosition ( localPlayer ) --Get player's position setElementPosition ( localPlayer, x+(math.random(-10,10)), y+(math.random(-10,10)), z+(math.random(1,15)) ) --Move a player randomly to a nearby location. X is current x + a number between -10, 10 and so on. end addCommandHandler ( "escape", escapeMe ) --When player types "/escape" in chatbox or "escape" in console I took this code from example from here https://wiki.multitheftauto.com/wiki/AddCommandHandler I have completely no idea why client side script is not working at all. I tried different this and nothing works. MTASA v1.5.9 server