Insert into database using knex - javascript

I am using knex 0.13.0 and I am trying to insert into a mysql database with the following function:
async function create(title, description) {
//trim spaces
console.log("title: " + title)
console.log("description: " + description)
title = title.trim()
description = description.trim()
createdAt = _.now()
deleted = false
console.log("Create Post: " + title + " " + description + " " + createdAt + " " + deleted)
if (title.length < 1 || title.length > 255) throw new Error('Title is not valid.')
if (description.length < 1) throw new Error('Description is not valid.')
try {
await knex('posts').insert({
title,
description,
createdAt,
deleted
})
console.log("added to db")
return true;
} catch (e) {
return "An error occured: " + e;
}
}
The last console output with Create Post: Title Description 1505062847788 falseis shown right, but nothing is happening, even after waiting
I guess it is the asynch part of the function, but what else to do in the meanwhile?
Is there a standard way to create an entry when using knex?
Appreciate your reply!

I'm using Node 6, so can't test 'await' at the moment (came in node 7) but from this post it looks like you should assign the await response to a variable. Like:
...
var awResponse; // new variable
try {
awResponse = await knex('posts').insert({
...
In detail:
async function create(title, description) {
//trim spaces
console.log("title: " + title)
console.log("description: " + description)
title = title.trim()
description = description.trim()
createdAt = _.now()
deleted = false
console.log("Create Post: " + title + " " + description + " " + createdAt + " " + deleted)
if (title.length < 1 || title.length > 255) throw new Error('Title is not valid.')
if (description.length < 1) throw new Error('Description is not valid.')
var awResponse; // new variable
try {
awResponse = await knex('posts').insert({
title,
description,
createdAt,
deleted
})
console.log("added to db")
return true;
} catch (e) {
return "An error occured: " + e;
}
}
What you have should work just fine, but what I've been doing (as an alternative for you) is just directly using promises, and constructing my data access functions generally as follows:
function create(title, description) {
return Promise.resolve().then(function () {
// This first section is for preping the record for insert.
//
//trim spaces
console.log("title: " + title)
console.log("description: " + description)
title = title.trim()
description = description.trim()
// createdAt = _.now() // I have a error that "_" is not valid
createdAt = (new Date()).toISOString();
deleted = false
console.log("Create Post: " + title + " " + description + " " + createdAt + " " + deleted)
if (title.length < 1 || title.length > 255) throw new Error('Title is not valid.')
if (description.length < 1) throw new Error('Description is not valid.')
return { "title": title,
"description": description,
"createdAt": createdAt,
"deleted": deleted };
})
.then(function (recordToInsert) {
// This second section is for the insert.
//
console.log("Part #2");
return knex('posts').insert(recordToInsert)
.on('query-error', function(ex, obj) {
// console.log("KNEX query-error ex:", ex);
// console.log("KNEX query-error obj:", obj);
// Below logs db errors into my custom encapsulation of winston logging.
// ... and the .catch further down will still be executed.
log.logMsg('error', "DBA.INS88", "KNEX create.on.query-error", {"fnc": "create", "obj":obj, "ex":ex} );
})
})
.then(function (insertResult) {
// This third section is for post-processing the result (if needed).
//
console.log("Part #3 added to db :", insertResult);
return insertResult; // returns id value from insert;
})
.catch(function (e) {
// I omit this .catch to let the caller know about and handle the exceptions
console.log( "An error occured: " + e);
});
};
Hope this helps!

Related

JS await not working as expected with pg query

I want to use the return of a query to a postgresSQL database. I don't want to just print it. I want to use it in another function. The problem is that the function here returns before it is finished with executing the code.
async function create_base_config(user_id, service_id, timer_seconds) {
var ret
var line
await db_adm_conn.query(`
INSERT INTO base_config (user_id, service_id, timer_seconds)
VALUES ('` + user_id + "', '" + service_id + "', '" + timer_seconds + "') RETURNING id;", (err, result) => {
if (err) {
ret = false
line = err
console.log("line2 err : " + line)
}
else {
ret = true
line = result.rows
console.log("line2 : " + line)
// json_return = JSON.parse(result)
// console.log(result.rows)
}
});
console.log("line: " + line)
return { ret_value: ret, line_value: line };
}
To test it i inserted debug prints. The output is:
server_1 | line: undefined
server_1 | line2 : [object Object]
so I am exectuting the code after the await before the await is finished. How can i fix that, so that he first executes all the code from the await and then the rest?
you should not use a callback function as the arrow function. You should use try catch like this:
async function create_base_config(user_id, service_id, timer_seconds) {
var ret
var line
try {
line = await db_adm_conn.query(`
INSERT INTO base_config (user_id, service_id, timer_seconds)
VALUES ('` + user_id + "', '" + service_id + "', '" + timer_seconds + "') RETURNING id;")
ret = true
}
catch (err) {
ret = false
line = err
}
return { ret_value: ret, line_value: line };
}

TypeError: makeUser is not a function discord js V12

(node:9436) UnhandledPromiseRejectionWarning: TypeError: makeUser is not a function
Hello I tried to make a command for when the person does the command automatically adds 1 line in a cfg called users.json but this gives me this error.
Again, as I was updating my bot to discord.js V12 I got an error:
TypeError: makeUser is not a function
Here is a part the code:
if (!config.servers.servidores.includes(args[4])) {
console.log(yellow + "[" + moment.tz("America/Sao_Paulo").format('HH:mm A') + "] " + red + `[DB]` + reset + ` ${msg.author.username} ` + l7yellow + `->` + lightred + ` Configuração Errada` + reset + ` Status: ` + red + `Servidor` + reset)
const metodoembed = new Discord.RichEmbed()
.setColor("#ff0000")
.setTitle("Database Editor -> Atom")
.setDescription("An `error` was identified when\nmodifying the database.")
.addField('**Type:**', '```http\n' + 'Server' + '```', true)
.addField('Solution:', 'Check the available\nservers in the database', true)
.setThumbnail(`${msg.author.displayAvatarURL}`)
.setTimestamp()
.setFooter(`${msg.author.username}`, `${msg.author.displayAvatarURL}`);
return msg.channel.send(metodoembed); }
//
function updategive(file, json) {
fs.writeFile(file, JSON.stringify(json, null, 2), "utf8", function(err) {
console.log(purple + "[" + reset + moment.tz("America/Sao_Paulo").format('HH:mm A') + purple + "] " + `[🧮] Banco de Dados atualizado.`);
});
}
if (!users[person]) {
makeUser(person);
}
users[person].attackTime = time;
users[person].concurrents = conc;
users[person].servers = servers;
users[person].expire = moment(expire).unix();
updategive("users.json", users);
return msg.channel.send(responsegive);
}
The error is occuring in this line:
if (!users[person]) {
makeUser(person);
}
Hello seems like you are using a function that is not existing. Create a function with the name makeUser and the parameter user. So it should look like this function makeUser(user) {...}.
Also if you are using discord.js v12, make sure you replace RichEmbed with MessageEmbed.

Statement always true

I have a script where if there is a second argument the script will run else it will run an else statement but even if there is no second argument it will always run the script
if(member && args.slice(1) !== undefined)
{
member.kick(args.slice(1).join(' ')).then(() =>{
message.channel.send("Successfully kicked " + "`" + user.tag + "`" +" for " + "**" +args.slice(1).join(' ') + "**" + " 🙂")
}).catch(err =>{
channel.message.send("An unexpected error occured. Logs were sent to the devs")
console.log(err);
return;
});
}else{
if(member){
member.kick().then(() =>{
message.channel.send("Successfullys kicked " + "`" + user.tag + "`")
console.log (args[2], args)
}).catch(err =>{
channel.message.send("An unexpected error occured. Logs were sent to the devs")
console.log(err);
return;
});
Assuming args is an array, .slice() will always return an array and never undefined. You should check for the length directly:
if (member) {
const kick = args.length > 1 ? member.kick(args.slice(1).join(' ')) : member.kick();
kick.then(() =>{
message.channel.send("Successfully kicked " + "`" + user.tag + "`" +" for " + "**" +args.slice(1).join(' ') + "**" + " 🙂")
}).catch(err =>{
channel.message.send("An unexpected error occured. Logs were sent to the devs")
console.log(err);
});
}
args.slice(1) will never be undefined. If the array args is too short, its result will be the empty array []. This is different from undefined.

Loop through Array and perform My SQL query based on result of previous query for each entry in the array

I have an array of items:
var myArr = ['item1', 'item2', 'item3'];
I'm attempting to loop over these items and check if they exist in my database. If the item does not exist, then I add it to the database.
var sql = 'Select * from DB where item="' + myArr[i] + '"';
connection.query(sql, function(e, r, f) {
if(!e && r.length <= 0) {
performInsertOnDB(myArr[i]);
}
});
My trouble is, the reference to variable i will not stay as connnection.query is asynchronous. I need to wait until the first select finishes before I can continue. I'm trying to use the Async library to accomplish this, but I must not be fully grasping how to perform the task.
This is what I have so far:
async.each(lootArray, function(lootItem, addLootItem) {
var sql = "SELECT * FROM loot_history WHERE date = DATE('" + moment(lootItem[1]).format('YYYY-MM-DD') + "') AND time = '" + lootItem[2] + "' AND itemId = " + lootItem[4];
connection.query(sql, function(error, results, fields) {
if (error) {
sendDiscordMessage(loachannel, error + ', <#105094681141977088>');
return false;
} else {
if (results.length > 0) {
//duplicates.push(lootArray[i]);
} else {
addLootItem(lootItem);
}
}
});
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
function addLootItem(lootItem) {
var sql = "INSERT INTO loot_history SET player = " + lootItem[0] + ", date = " + moment(lootItem[1]).format('YYYY-MM-DD') + ", time = '" + lootItem[2] + ", item = " + lootItem[3] + ", itemId = " + lootItem[4] + ", itemString=" + lootItem[5] + ", response= " + lootItem[6] + ", votes= " + lootItem[7] + ", class= " + lootItem[8] + ", instance=" + lootItem[9] + ", boss=" + lootItem[10] + ", gear1=" + lootItem[11] + ", gear2=" + lootItem[12] + ", reasponseId=" + lootItem[13] + ", isAwardReason=" + lootItem[14];
connection.query(sql, function(error, results, fields) {
if (error) {
sendDiscordMessage(loachannel, error + ', <#105094681141977088>');
}
});
}
EDIT: Everything works, except the callback AddLootItem is not firing. Why is this callback not getting called? I can set log events in that if statement that execute, but the function itself never fires.
The problem is that the name of async callback is the same as the function you want to be called when the item does not exist. Try to change the name in the function to something else let's say: callback, and call it in your if statement or pass it to addLootItem, and call it there once the item added.
async.each(lootArray, function(lootItem, callback) {
var sql = "SELECT * FROM loot_history WHERE date = DATE('" + moment(lootItem[1]).format('YYYY-MM-DD') + "') AND time = '" + lootItem[2] + "' AND itemId = " + lootItem[4];
connection.query(sql, function(error, results, fields) {
if (error) {
sendDiscordMessage(loachannel, error + ', <#105094681141977088>');
calback(err);
} else {
if (results.length > 0) {
//duplicates.push(lootArray[i]);
callback();
} else {
addLootItem(lootItem, callback);
}
}
});
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
function addLootItem(lootItem, done) {
var sql = "INSERT INTO loot_history SET player = " + lootItem[0] + ", date = " + moment(lootItem[1]).format('YYYY-MM-DD') + ", time = '" + lootItem[2] + ", item = " + lootItem[3] + ", itemId = " + lootItem[4] + ", itemString=" + lootItem[5] + ", response= " + lootItem[6] + ", votes= " + lootItem[7] + ", class= " + lootItem[8] + ", instance=" + lootItem[9] + ", boss=" + lootItem[10] + ", gear1=" + lootItem[11] + ", gear2=" + lootItem[12] + ", reasponseId=" + lootItem[13] + ", isAwardReason=" + lootItem[14];
connection.query(sql, function(error, results, fields) {
if (error) {
sendDiscordMessage(loachannel, error + ', <#105094681141977088>');
}
done();
});
}

relational database and oop javascript

i am trying to create a relational database while using oop in java script , yet i am encountered with some difficulties , this is the code ,
IT WAS WORKING BEFORE I CHANGED IT TO OOP
function DB() {
this.size;
this.row;
this.getsize = function() {
return this.size;
}
this.db = window.openDatabase('coupons', "1.0", 'database for coupons', 100000);
this.al = function() {
alert('al works');
}
this.add = function(table, id, name, email, fav) {
// alert("works");
// alert("INSERT INTO " + table + " VALUES(" + id + "," + name + ")");
this.db.transaction(function(ob)
{
ob.executeSql("SELECT * FROM " + table + " WHERE pid= " + id + "", [], this.dataHandler, this.errorHandler);
});
this.db.transaction(function(ob)
{
//alert(getsize());
if (this.size > 0) {
alert("user already exists")
} else {
ob.executeSql("CREATE TABLE IF NOT EXISTS " + table + " (pid INTEGER, pname TEXT, pemail TEXT,pfav)");
ob.executeSql("INSERT INTO " + table + " VALUES(" + id + "," + "'" + name + "'" + "," + "'" + email + "'" + "," + "'" + fav + "'" + ")");
alert("user addd successfuly");
}
}
);
}
this.errorHandler = function(error)
{
document.write("handling error " + error);
}
this.dataHandler = function(transaction, data)
{
// document.write("<table>");
//document.write("<tr><th>id</th><th>name</th></tr>")
// size = data.rows.length;
//for(i=0;i<size;i++)
// {
//Variables.call(this,data.rows.length,data.rows.item(0));
//Variables.call(7,6);
this.size = data.rows.length;
this.row = data.rows.item(0);
//return row;
// document.write(
// "<tr><td>"+row['pid']+"</td><td>"+row['pname']+"</td></tr>");
// }
//document.write("</table>");
}
this.getrows = function(n)
{
switch (n)
{
case 'pid':
return this.row['pid'];
break;
case 'pname':
return this.row['pname'];
break;
case 'pemail':
return this.row['pemail'];
break;
case 'pfav':
return this.row['pfav'];
break;
default:
}
}
}
the problem are as follows , hope you can help me out !!
1.after calling the function add , it does not go to dataHandler function .
2. in the add function i am unable to use local variables , how can i use the variable 'size' to check if the user exists in the database or not ?!! ,
hope you can help i have been in this code for 2 days !!! :(
Yes. You obviously can't access this.size in your function because you are using a anonymous function, so this is not related to your DB -oject but points to that anonymous function.
The same for your calls to this.dataHandler or this.errorHandler.
So you could just
this.db.transaction = function(ob)
to make it a method of your object which then will give you full access to the this - pointer of your DB - Object.
EDIT: Sorry, this would then point to the db object, of course, so this is not a solution.
But you can pass it your data - and errorHandler like this:
this.db.transaction(function() { ... }, this.errorHandler, this.dataHandler);
and avoid the call to this.size within the second transaction - statement by simply wrapping your call like:
if(this.size > 0) { alert('..'); } else { db.transaction(...) }
But: Your errorHandler and dataHandler must actually correspond to the right interface - definitions, take a look at:
http://www.w3.org/TR/2009/WD-html5-20090212/structured-client-side-storage.html

Categories

Resources