Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  175] [ 1]  / answers: 1 / hits: 20545  / 6 Years ago, wed, august 15, 2018, 12:00:00

I am trying to create a bot for the discord platform that will search a SQL database and return the objects to the chat for people to see.


SQL uses promises and I have been unable to successfully turn the promise it is returning me into something I can return to the chat (a string or array).


This code queries the database:


function spell(name) {
var spellData = sql.get("SELECT * FROM spells WHERE LOWER(name) = '"+ name.toLowerCase() + "'");
spellData.then( value => {
console.log(spellData)
return spellData;
});

}

The table:


CREATE TABLE spells (
`name` VARCHAR(25),
`casting_time` VARCHAR(95),
`components` VARCHAR(215),
`description` VARCHAR(3307),
`duration` VARCHAR(52),
`level` INT,
`range` VARCHAR(28),
`school` VARCHAR(13)
);

I'm using node.js, sqlite, and discord.js.


More From » node.js

 Answers
3

If you want to return the Promise object to the caller, simply do:



function spell(name) {
return sql.get(SELECT * FROM spells WHERE LOWER(name) = ' + name.toLowerCase() + ')
}


Then, in your client:



spell('some_name').then(function(result) { console.log(result); })


Or, if you're into awaiting:



let results = await spell('some_name')
console.log(results)


Don't know if you're making use of it or not but parameterized queries will guard against SQL injection attacks. Your NPM package of choice should have an adequately managed implementation.


[#53731] Friday, August 10, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sashacitlallik

Total Points: 30
Total Questions: 100
Total Answers: 85

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
;