I write some code in the server-side, in node js for update MySQL database.
this is my code:
exports.addPlayerToMatch = function(uid,matchId){
return new Promise(function(resolve,reject){
dbFunctions.getPlayerUids(matchId).then(function(rows){
playerUids = JSON.parse(rows[0].uids_of_players);
playerUids.push(uid);
console.log("new player uids: " +playerUids);
numberOfPlayers = playerUids.length;
db.query('UPDATE current_matches SET number_of_players = ?,
uids_of_players = ? WHERE id = ?' ,[numberOfPlayers,playerUids,matchId],
function (err, rows) {
if (err){ console.log("ErrorForAddPlayerToMatch: "+err);}
});
resolve(numberOfPlayers);
});
});
};
and this is the error:
ErrorForAddPlayerToMatch: Error: ER_PARSE_ERROR: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''3d8b7210fc1999bf191b0e1f7d744269' WHERE id = 29' at line 1
I have no idea !!
help, please.
The Problem was that I forgot to turn an array into a JSON object again before using it in the query.
it will solve with something like this:
var playerUids = JSON.stringify(playerUids);
and then use it in the query.
Related
I managed to display data from database next I try to add data to my database here is my code snippet >>>
exports.tambahData = (req, res) => {
var keyy = req.body.keyy;
var valuee = req.body.valuee;
var brand = req.body.brand;
var productName = req.body.productName;
connection.query(
"INSERT INTO `keranjang` (keyy,valuee,brand,productName) VALUES(?,?,?,?,)",
[keyy, valuee, brand, productName],
(err, rows, fields) => {
if (err) {
console.log(err);
} else {
response.ok("successfully added ", res);
}
}
);
};
but i getting an error below
ERROR:er_parse_error: you have an error in your sql syntax; check the manual that corresponds to your mariadb server version for the right syntax to use near
Any Ideas on how to solve it
You have an additional comma (,) in your sql statement
INSERT INTO `keranjang` (keyy,valuee,brand,productName) VALUES(?,?,?,?,)
Try changing it to
INSERT INTO `keranjang` (keyy,valuee,brand,productName) VALUES(?,?,?,?)
Let me know if this works for you.
"INSERT INTO keranjang (keyy,valuee,brand,productName) VALUES(?,?,?,?,)",
Correct this line to
"INSERT INTO keranjang (keyy,valuee,brand,productName) VALUES(?,?,?,?)",
I am attempting to perform an update to a MongoDB document (using mongoose) by first using .findById to get the document, then updating the fields in that document with new values. I am still a bit new to this so I used a tutorial to figure out how to get it working, then I have been updating my code for my needs. Here is the tutorial: MEAN App Tutorial with Angular 4. The original code had a schema defined, but my requirement is for a generic MongoDB interface that will simply take whatever payload is sent to it and send it along to MongoDB. The original tutorial had something like this:
exports.updateTodo = async function(todo){
var id = todo.id
try{
//Find the old Todo Object by the Id
var oldTodo = await ToDo.findById(id);
}catch(e){
throw Error("Error occured while Finding the Todo")
}
// If no old Todo Object exists return false
if(!oldTodo){
return false;
}
console.log(oldTodo)
//Edit the Todo Object
oldTodo.title = todo.title
oldTodo.description = todo.description
oldTodo.status = todo.status
console.log(oldTodo)
try{
var savedTodo = await oldTodo.save()
return savedTodo;
}catch(e){
throw Error("And Error occured while updating the Todo");
}
}
However, since I don't want a schema and want to allow anything through, I don't want to assign static values to specific field names like, title, description, status, etc. So, I came up with this:
exports.updateData = async function(update){
var id = update.id
// Check the existence of the query parameters, If they don't exist then assign a default value
var dbName = update.dbName ? update.dbName : 'test'
var collection = update.collection ? update.collection : 'testing';
const Test = mongoose.model(dbName, TestSchema, collection);
try{
//Find the existing Test object by the Id
var existingData = await Test.findById(id);
}catch(e){
throw Error("Error occurred while finding the Test document - " + e)
}
// If no existing Test object exists return false
if(!existingData){
return false;
}
console.log("Existing document is " + existingData)
//Edit the Test object
existingData = JSON.parse(JSON.stringify(update))
//This was another way to overwrite existing field values, but
//performs a "shallow copy" so it's not desireable
//existingData = Object.assign({}, existingData, update)
//existingData.title = update.title
//existingData.description = update.description
//existingData.status = update.status
console.log("New data is " + existingData)
try{
var savedOutput = await existingData.save()
return savedOutput;
}catch(e){
throw Error("An error occurred while updating the Test document - " + e);
}
}
My original problem with this was that I had a lot of issues getting the new values to overwrite the old ones. Now that that's been solved, I am getting the error of "TypeError: existingData.save is not a function". I am thinking the data type changed or something, and now it is not being accepted. When I uncomment the static values that were in the old tutorial code, it works. This is further supported by my console logging before and after I join the objects, because the first one prints the actual data and the second one prints [object Object]. However, I can't seem to figure out what it's expecting. Any help would be greatly appreciated.
EDIT: I figured it out. Apparently Mongoose has its own data type of "Model" which gets changed if you do anything crazy to the underlying data by using things like JSON.stringify. I used Object.prototype.constructor to figure out the actual object type like so:
console.log("THIS IS BEFORE: " + existingData.constructor);
existingData = JSON.parse(JSON.stringify(update));
console.log("THIS IS AFTER: " + existingData.constructor);
And I got this:
THIS IS BEFORE: function model(doc, fields, skipId) {
model.hooks.execPreSync('createModel', doc);
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
Model.call(this, doc, fields, skipId);
}
THIS IS AFTER: function Object() { [native code] }
Which showed me what was actually going on. I added this to fix it:
existingData = new Test(JSON.parse(JSON.stringify(update)));
On a related note, I should probably just use the native MongoDB driver at this point, but it's working, so I'll just put it on my to do list for now.
You've now found a solution but I would suggest using the MongoDB driver which would make your code look something along the lines of this and would make the origional issue disappear:
// MongoDB Settings
const MongoClient = require(`mongodb`).MongoClient;
const mongodb_uri = `mongodb+srv://${REPLACE_mongodb_username}:${REPLACE_mongodb_password}#url-here.gcp.mongodb.net/test`;
const db_name = `test`;
let db; // allows us to reuse the database connection once it is opened
// Open MongoDB Connection
const open_database_connection = async () => {
try {
client = await MongoClient.connect(mongodb_uri);
} catch (err) { throw new Error(err); }
db = client.db(db_name);
};
exports.updateData = async update => {
// open database connection if it isn't already open
try {
if (!db) await open_database_connection();
} catch (err) { throw new Error(err); }
// update document
let savedOutput;
try {
savedOutput = await db.collection(`testing`).updateOne( // .save() is being depreciated
{ // filter
_id: update.id // the '_id' might need to be 'id' depending on how you have set your collection up, usually it is '_id'
},
$set: { // I've assumed that you are overwriting the fields you are updating hence the '$set' operator
update // update here - this is assuming that the update object only contains fields that should be updated
}
// If you want to add a new document if the id isn't found add the below line
// ,{ upsert: true }
);
} catch (err) { throw new Error(`An error occurred while updating the Test document - ${err}`); }
if (savedOutput.matchedCount !== 1) return false; // if you add in '{ upsert: true }' above, then remove this line as it will create a new document
return savedOutput;
}
The collection testing would need to be created before this code but this is only a one-time thing and is very easy - if you are using MongoDB Atlas then you can use MongoDB Compass / go in your online admin to create the collection without a single line of code...
As far as I can see you should need to duplicate the update object. The above reduces the database calls from 2 to one and allows you to reuse the database connection, potentially anywhere else in the application which would help to speed things up. Also don't store your MongoDB credentials directly in the code.
I have been developing an app with ionic. And I have used SQLite. I have executed a query but I can't console log the value. Here's the code snippet:
var query = "SELECT SUM(total) FROM items";
console.log(query);
$cordovaSQLite.execute(db, query, []).then(function (res) {
console.log(res.rows[0]);
$scope.grand = {};
$scope.grand = res.rows[0];
console.log($scope.grand.SUM);
}, function (err) {
console.error("error=>" + err);
});
I want to directly console log the value of SUM(total). But, the log shows like below:
Object {SUM(total): 400}
SUM(total):400
How do I directly console log 400?
I would guess it should work like this:
console.log(res.rows[0]['SUM(total)']);
Because I always feel uncomfortable using characters like parentheses in a key name I would maybe prefer:
var query = "SELECT SUM(total) as mytotal FROM items";
and then:
console.log(res.rows[0]['mytotal']);
or
console.log(res.rows[0].mytotal);
I'm trying to figure out how to do dynamic queries in node.js with node-mysql. Which table and fields to insert and update is based on users' request from the data object. I thought of creating a module to store different queries like the following but no luck. It gave me this error:
TypeError: Object function (){
var likes = 'INSERT IGNORE INTO item (id,like_type) VALUES(?,?)';
return likes;
} has no method 'replace'
app.js
var queries = require('./queries');
.....
socket.on("like",function(data){
var table_file = data[0].table;
var d = data[1];
connection.query(queries[table_file]
,[d.id,d.like_type],function(err,rows,fields){
if(err){ throw err;}
console.log(rows);
});
queries.js:
module.exports = {
item_like:function(){
var likes = 'INSERT IGNORE INTO item (id,like_type) VALUES(?,?)';
return likes;
},
people:function(){
var likes = 'INSERT IGNORE INTO people (id,like_type) VALUES(?,?)';
return likes;
}
}
Here's the data object sent to socket:
data object: {table:item_like},{id:1,like_type:1};
Change your exports in queries.js to be set to the SQL strings instead of functions, since that is how you're treating them currently:
module.exports = {
item_like: 'INSERT IGNORE INTO item (id,like_type) VALUES(?,?)',
people: 'INSERT IGNORE INTO people (id,like_type) VALUES(?,?)'
}
I am trying to call a stored procedure in java from my .xsjs file. The procedure gets 2 input parameters and returns one. I can call it from an SQL console without any problem by writing:
call "MYSCHEMA"."MyPackage.procedures::addUserC50" ('name', 'lastname', ?)
This is the code in my .xsjs file, I think that it fails in the prepareCall statement. I have tried different combinations (with and without double quotation marks, with and without the name of the schema/package, with and without "(?,?,?)", with and without "{ }".... But nothing works, I always get the 500 Internal server error when I try to execute it.
Does anybody know where the error is or what is the exact syntax for the prepareCall method?
var output = 0,
query = "";
var conn;
var cstmt;
try {
conn = $.db.getConnection();
query = "{ call \"MYSCHEMA\".\"MyPackage.procedures/addUserC50\"(?,?,?) }";
cstmt = conn.prepareCall(query); // <-- Fails
cstmt.setString(1, userName);
cstmt.setString(2, userLastname);
cstmt.execute();
output = cstmt.getInteger(3);
conn.commit();
$.response.status = $.net.http.OK;
$.response.setBody("Successfully created: " + output);
}
catch (e) {
$.response.status = $.net.http.BAD_REQUEST;
$.response.setBody("0");
}
finally {
if (cstmt !== null)
cstmt.close();
if (conn !== null)
conn.close();
}
This is the error that gives back: InternalError: dberror(Connection.prepareCall): 328 - invalid name of function or procedure: MyPackage.procedures/addUserC50: line 1 col 18 (at pos 17) at ptime/query/checker/check_call.cc:21
According to this documentation, it should be something like
var myCallableStatement = myconnection.prepareCall("{call myprocedure(?)}");
Thank you
I managed to make it run, this is the syntax that worked:
query = "call \"MyPackage.procedures::addUserC50\"(?, ?, ?)";
Thank you for your help #shofmn
There could be different reasons why the call is failing. You can investigate your error much easier if you return the error message in the HTTP response. You can do this easily:
try {
// Your code execution here
}
catch (e) {
$.response.contentType = "text/html";
$.response.setBody(e.name + ": " + e.message));
}
If the error message doesn't help you solving the problem, paste the error message in here so that it is more easy for us to investigate as well.