AlaSQL set result to variable for use elsewhere - javascript

I'm fairly new to JavaScript and AlaSQL, so sorry if this is obvious.
I'm trying to save the result of my AlaSQL query to a variable so I can use it elsewhere in my code. As a bit of context, I'm querying a CSV file, and I'm wanting to get the max, min and a certain row of a particular column, so I can then use this elsewhere. At the moment I'm just trying it on the max aspect.
<script>
alasql.promise('VALUE OF SELECT MAX(PM1) FROM CSV("http://localhost:8000/assets/interpolated.csv", {separator:","})').then(function(res){console.log(res); document.getElementById("testingId5").innerHTML = res}).catch(function(err){console.log('Error:', err);});
</script>
This produces a result and logs it in the console, and shows it onscreen, but I want to set it to a variable to be able to use too!
I've tried just adding var test = res; within the .then(function(res){}) part, but with no luck.
Thanks in advance.

Related

Size of jquery.fn.init changes unexpectedly

I'm trying to parse HTML code in jquery, get an attribute of it and extract a number from the id, and then append it to a string like that:
function sortHTMLContentByStrength(x, y) {
console.log($($.parseHTML(x)));
let strength1 =
parseFloat(($("#team-modal-strength-" + $($.parseHTML(x)[1]).attr("id").match(/\d+/)[0]).html()));
let strength2 =
parseFloat(($("#team-modal-strength-" + $($.parseHTML(y)[1]).attr("id").match(/\d+/)[0]).html()));
return strength2 - strength1;
}
The real problem is:
As you can see, i get this S.fn.init(2). Every single console log looks like this. I roughly know what it means and also it wouldn't be a big deal because I used an index to retrieve only the second element (tr tag) which I'm interested in like that:
$($.parseHTML(x)[1])
I'm just getting the second element of the returned array which is tr tag. This code worked and there was nothing wrong with up until now.
So, now the biggest problem. When I run the same code once more later, I don't receive an array of two objects anymore. I get this:
It's not an array anymore so the indexing trick $($.parseHTML(x)[1]) doesn't work anymore.
I really need to call this sorting function multiple times in my program but due to the issue the app breaks. I would be very thankful for any hints on that.

Cannot put a variable inside channels.get() on discord.js

I've been trying to do some workaround on this thing but it just drives me crazy. The way I see it, it should work just fine but there is something going wrong with it... I hope I am not just really tired and missed a very easy mistake.
Anyway here is the thing. It's something pretty easy to do. an event is activated and then the algorithm below is supposed to read a certain database where I have the ids of the channels I want to send to stored, and it does one loop for each of those ids, outputting a different id on each loop,
(note: I am not doing it inside a client.on("message) event)
let channel = client.channels.get(dbresult)
channel.send(`test`);
This part was supposed to run 2 times with 2 separate numbers on the dbresult variable(since i have just 2 numbers on the database).
Now, when I run this part with just the id number alone and not the dbresult variable like this:
rows.forEach(function(row){
//let dbresult = row.newsid.toString()
let channel = client.channels.get(`0123456789`)
channel.send(`test`);
})
the script works just fine, but when I use the dbresult variable, it gives me this error
channel.send(`test`);
^
TypeError: Cannot read property 'send' of undefined
Now these are the things I've tried:
Putting a console.log(dbresult) just inside the foreach() block. It reads the ids just fine!
Making dbresult a string (as you can see) and also I've tried not converting it into a string too
Just Because I was desperate I even tried this: channel.send(${dbresult}), just in case.
Things that work:
The things I want to get triggered outside this part of the script are just fine, the script always gets activated successfully.
The script always reads each and every one of the ids of the channel. By removing the insides of "foreach" function and putting a console.log(dbresult), I saw that it outputs the different ids I want it to output.
Here is the whole script:
let db = new sqlite.Database('./databases/Serverinfo', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
const sql = 'SELECT newsid FROM news';
db.all(sql, function(error,rows){
if (rows.length <= 0){
return;
}
if (error){
throw error;
}
rows.forEach(function(row){
let dbresult = row.newsid.toString()
console.log(dbresult)
let channel = client.channels.get(dbresult)
channel.send(`test`);
})
db.close();
})
Any help is appriciated of course
If you are using discord.js v.12 you need to use the cache collection.
And yes, the id you want to use needs to be a string.
let channel = client.channels.cache.get(dbresult);
https://discord.js.org/#/docs/main/stable/class/ChannelManager
What you might consider doing is checking if the channel was found before you try to send something.
EDIT: The problem after updating comes from the fact that you store the channelID in your database as an INT which brings the following problem: Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers.
What that means is that it doesn't store the ID correctly because it is too large, we are talking in the Quadrillions here. This could happen either at your database or when you import the number into your code. Without knowing your DB setup I can't tell.
Example:
683328323304292410 // the actual ID of the channel
683328323304292400 // the INT that is stored
To fix this you have two options and both need to be implemented in your database.
You can either convert the datatype for the row which stores the channelID to bigINT or convert it when you import the ID source
You can store the channelID as a string in the first place. (probably the easier solution)

How I get access to new intervals using "getNextDataRange()" of Selection Class?

I'm facing an error when I try to get values using the Method "getNextDataRange(Direction)" but I can't understand where I missed out.
first: I Opened another spreadsheet and selected the first sheet. I SET "B2" as initial current cell. Then I back to select the actual sheet to get access of "Selection" Class, so finally I can use "getNextDataRange":
Heres my code:
function ok() {
var sheets = SpreadsheetApp.openById("SHEET ID").getSheets()
var firstsheet = sheets[0].getRange("B2").activateAsCurrentCell().getSheet().getSelection().getNextDataRange(SpreadsheetApp.Direction.RIGHT).getValues
return SpreadsheetApp.getActiveSpreadsheet().getRange("B1").activate().setValue(firstsheet[0][0])
}
--
3. Then I wanna bring the values obtained in the range select by the getNextDataRange to import to my actual spreadsheet. Start from the range "B1" and taking the first line and the first column of the selection that I got previously.
The fallow error occours:
Cannot find getNextDataRange ((class)) method.
Someone has already faced that? I appreciate if someone could explain what detail I missed. I believe that is that doubt of many other too.

JavaScript + MySQL: use fields as parameter in result

I'm quite new to JavaScript and I have the following issue:
I have a Node.JS server on which a webclient can connect and execute functions. One function is to look into a MySQL database and gather information.
The query is done right and I obtain the correct raw information as for example:
Here is my code:
So I correctly get the column names using the fields (fields[0].name = Count_0)variable and I am able to get the correct value using the result (result[0].Count_0 = Gray).
However, I am unable to merge the two lines in order to create the list of colors using something like this in a loop: result[0].fields[0].name = Gray
Is there an easier way to do this or not ?
Thanks,
Nicola.
In Javascript, you can use the [] operator to access a variably-named property in an object.
Instead of using result[0].fields[0].name, use
result[0][fields[0].name]
You won't get any runtime errors for accessing a property that doesn't exist, so you'll want to check whether that value is undefined before using it somewhere else.
It seems you want to get the color. If so, you can get the color by this
let color = result[0][fields[0].name];
The idea is use fields[0].name as key of result[0].
This is the breakdown of above single line.
let key = fields[0].name;
let color = result[0][key];

Calling arrays and data in D3

I've been coding in D3 from a while now but there's bits about it that still leave me stumped. I've got some data in a csv file. I'm calling it in the following way
var dataset;
d3.tsv("ST_03_data.tsv", function(data) {
dataset=data;
and then I do a whole bunch of things like drawing a graph or whatever and it's fine. I'd like to use some, but not all, of the columns of data in a certain way, so what I'd like to do is set them up as a variable. So what I've done, straight away underneath the code above is write
var newvar = (d.data1, d.data3, d.data5)
where data1, data3 and data5 are columns of data. get the error message "Can't find variable d". If if use (data1, data3, data5) instead (that is, dropping the d.) I get an equivalent message.
So what's going wrong? Do I need to build a function? I've sort of tried and that hasn't worked either but can see how it might be better. All help appreciated. Thanks.
There's a full example here -http://www.graphitti.org/admin2/files/experiments/click_counter3.html It looks like it works and it does up to a point but I want to make it easier. At the moment, there are two clicks, and clicking them passes new data in. It does so using variables I have set up in each click, in fact in the attributes for the rectangles. What I want to do is move that variable (called barup at the moment) to the top of the code so I don't need to repeat it.

Categories