Receiving String saved wrong in the database - javascript

I have an API using Express.js that receives data from Sigfox(A service for IoT) and inserts the data into the SQL Server Express database, this data comes in 4 different URL with this format:
http://servername:port/api/mediciones/sigfox_libelium/aire/2059E7/230934644ae17a8441/1626762070/9
My solution to insert all the data, is to create an array and fill it with the parsed data from the all 4 URL.
[
'2059E7',
64,
16.559999465942383,
98.3505859375,
100161.21875,
1.7199997901916504,
3.1599998474121094,
16.219999313354492
]
When the last request comes and the array is filled, an Insert is made into the database table:
.query("INSERT INTO medicionAire(fecha,id, bateria, temperatura, humedad, presionatmosferica, pm1, pm2, pm10) VALUES(SYSDATETIME()," + dimensiones[0] + ", "+ dimensiones[1] + ", " + dimensiones[2] + ", " + dimensiones[3] + ", " + dimensiones[4] + ", " + dimensiones[5] + ", " + dimensiones[6] + ", " + dimensiones[7] + ");")
where Dimensiones[] is the array filled with data.
ID should be "2059E7" like in the URL but when saved looks like this
EDIT: This is the Design of the table, column id is a Varchar, not a num, but looks like is being treated as a number

In your database the data type of id is a numeric type (float, int, decimal...) and therefore the value 2059E7, which is a mathematical notation for 2059 multiplied by 10000000 (7 zeroes).
SQL server returns a standard notation x.yEz or 2.059E+10.
You need to change the data type of the id column to be a text type (VARCHAR...)

Related

How to get data from a especif Time period in InfluxDB Node-RED

I'm trying to read data from InfluxDB v1.x but only in a especific time period, Example:
Get the values of temper01 from 2021-11-09/14:00:00 to 2021-11-10/14:00:00.
i know its possible with this code :
"WHERE time >= '2021-11-09T14:00:00Z' AND time <= '2021-11-10T14:00:00Z'" But i wanna know if it is possible to use a variable instead of the direct timestamp.
i've tried this code :
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= "
+ msg.payload;
+ " AND time <= '2021-11-18T00:00:00.000Z' " ;
but dosent matter the msg.payload value, it just get data from the very first data stored, and when i try to single quote msg.payload i get the invalid operation: time and *influxql.VarRef are not compatible" error message.
Assuming that msg.paylaod looks something like 2020-11-18T00:00:00.000Z then you have not quoted the time string you are inserting into the query.
It should look like this:
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= '"
+ msg.payload;
+ "' AND time <= '2021-11-18T00:00:00.000Z' " ;
The difference is the ' at the end of the string on the 3rd line and again at the start of the string on the last line.
Directly comparing a working version with the version that didn't work would have shown this.

How to get valid result from table.get using Dexie/IndexDB

I'm a PHP coder so not fully in the know with Javascript so possibly making a real schoolboy error here. I'm trying to facilitate some client side DB using Dexie & IndexDB. I can get values in using db.table.put and retrieve them all using db.table.toArray. However I seem to be struggling with getting individual records back out again. I'm trying to query for Keydata based on a form input. Heres my code, but it returns a TypeError cannot read property of undefined.
Here I can confirm in line 4 that my 'search2' variable contacts keyData (e.g. 1). But when used in line 5 it doesn't return a valid result. Can anyone see the issue?
function searchData(){
var search2;
search2 = document.getElementById('search2').value;
alert ("Id " + search2);
db.contacts.get(search2).then
(function (contact) {
alert ("Id " + contact.id + " : " + contact.title + " " + contact.forename + " " + contact.surname);
}).catch(function(error) {
alert ("Ooops: " + error);
});
}

I am unable to get environment names from an array

I have an array in which the environment list is stored and from this list, I have to get all the environment names which I am unable to get right now. See below what I have tried and please help me out of this.
<script>
//${environmentList} is getting from backend i.e from modal class in which toString() method has stored with all its contents.
var envLength = `${environmentList}`.length;
var allEnv = `${environmentList}`;
console.log("All Environments : " + allEnv);
console.log("Length of environments : " + envLength);
for(let i = 0 ; i < envLength ; i++){
let all = `${allEnv[i].environmentName}`;
console.log("Environment Name : " + all[i]);
}
</script>
toString() method :
#Override
public String toString() {
return "EnvironmentModel [environmentId=" + environmentId + ", environmentName=" + environmentName
+ ", environmentDesc=" + environmentDesc + ", hostAddress=" + hostAddress + ", environmentURL="
+ environmentURL + ", configFileName=" + configFileName + ", configFilePath=" + configFilePath
+ ", companyId=" + companyId + ", createdBy=" + createdBy + ", createdAt=" + createdAt
+ ", lastUpdatedBy=" + lastUpdatedBy + ", lastUpdatedAt=" + lastUpdatedAt + ", elementFileName="
+ elementFileName + ", elementFilePath=" + elementFilePath + ", envStatus=" + envStatus + ", projectId="
+ projectId + ", project=" + project + "]";
}
OUTPUT:
All Environments :
[EnvironmentModel [environmentId=345, environmentName=MM_Env_1, environmentDesc=Environment_1, hostAddress=null, environmentURL=null, configFileName=SystemFile.xml, configFilePath=/env_files/MM_Env_1, companyId=1, createdBy=2, createdAt=2019-12-18 16:39:38.595, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:42:13.259, elementFileName=ElementFile.xml, elementFilePath=/env_files/MM_Env_1, envStatus=1, projectId=null, project=[Project [projectName=Magic Matrix, projectDescription=Magic Matrix is a significant and robust tool of the Solitera Software Company., projectId=23]]], EnvironmentModel [environmentId=346, environmentName=MM_Env_2, environmentDesc=Environment_2, hostAddress=null, environmentURL=null, configFileName=soapAPI_request_GetLyrics.xml, configFilePath=/env_files/MM_Env_2, companyId=1, createdBy=2, createdAt=2019-12-18 16:43:10.59, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:43:58.522, elementFileName=Element.xml, elementFilePath=/env_files/MM_Env_2, envStatus=1, projectId=null, project=[Project [projectName=Magic Matrix, projectDescription=Magic Matrix is a significant and robust tool of the Solitera Software Company., projectId=23]]], EnvironmentModel [environmentId=347, environmentName=TM_Env_1, environmentDesc=TM_Env_1, hostAddress=null, environmentURL=null, configFileName=showcasetestData.xml, configFilePath=/env_files/TM_Env_1, companyId=1, createdBy=2, createdAt=2019-12-18 16:44:33.783, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:44:47.09, elementFileName=soapAPI_request_TempConvert.xml, elementFilePath=/env_files/TM_Env_1, envStatus=1, projectId=null, project=[Project ...
Length of environments : 2235
Environment Name : undefined
Use Jackson json parser in Java class and in javascript side use JSON.parse to change the string to JSON object.
On the javascript side you'll should understand that the variable environmentList is a string and the length of the string is 2235 chars. To access each property you'll have to parse the string and turn it into an object.
As you are not relying on JSON you need to use String.split or a regex matcher.
But the simpler solution would be to convert the Environment variables into a JSON format. As the type of the output from System.getenv() is a Map<string, string> you can try this solution to create a JSON structure: Converting Map<String,String> into json
After that you'll either write the json in a script tag in HTML so it get's parsed directly, or user JSON parse on the string of the above solution.

mysql parse error shows when query has 3 conditions

i have a select query to a local database and for some reason the following error shows up:
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 'FROM site WHERE name = OCC AND date_start = 2018-07-30 08:00:00 AND date_end = '' at line 1
here's my query:
connection.query("SELECT *, FROM shop WHERE name = " + shop_name + " AND date_start = " + myDate + " AND date_end = " + myDate2, function (err, result)
{
if (err)
{
console.log("Error Is:" + err);
}
else
{
console.log('DATA EXISTING IS =' + JSON.stringify(result));
}
});
am i missing something?
The usual mantra: use parameterized queries. They will prevent SQL injections and make your service more secure. Furthermore they will take care of the usual pitfalls when building a query using string concatenation.
Let's have a look at your query
"SELECT *, FROM shop WHERE name = " + shop_name + " AND date_start = " + myDate + " AND date_end = " + myDate2
Which spells out to something like
SELECT *, FROM shop WHERE name = myshop AND date_start = 2018-07-30 AND date_end = 2018-08-10
There are at least 3 errors
The , behind the SELECT * this is also the one the error tells you about. I suppose you had a column list and replaced it with *
The shop name column is most certainly some char column. So you have to enclose your values with quotes
Also the dates must be used with quotes, so the SQL engine will parse it to a date and do the comparison. For some SQL engines there is also a special annotation for dates. Have a look in the documentation.
This query should work
"SELECT * FROM shop WHERE name = '" + shop_name + "' AND date_start = '" + myDate + "' AND date_end = '" + myDate2 +"'"
depending on what myDate and myDate2 are.
At least problems 2 and 3 would not happen if you use parameterized queries. Consult the documentation of the library you are using.

Javascript variable, different values, same line

First of all, let me apologize for the title, as it isn't so explanatory, but I could not say it in another way.
The deal is: I am doing a javascript application, in which I have an object called "ocorrencia", which was defined like this:
var ocorrencia = new Object();
that object has several children, being filled by a method:
ocorrencia.idOcorrencia = ""+ year + month + day + hour + minute + second + milisec;
idOcorrencia is the one I am having problems with, because I am running a DataBase insert with this value, and I use it 2 times in the same insert, like:
var sql = 'INSERT INTO OCORRENCIAS (id, ocorrencia, data, resolucao, urgencia, foto) VALUES (' + ocorrencia.idOcorrencia + ', "' + ocorrencia.descricao + '", "' + ocorrencia.data + '", "' + ocorrencia.resolucao + '", "' + ocorrencia.grauUrg + '", "' + ocorrencia.idOcorrencia + '.jpg"' +')';
The insert runs great, an I have all the data inserted in the DB, BUT "id" and "foto" (which were supposed to get equal values) are giving me different values by 2 or 3 miliseconds.
How can this happen, as I am not changing "ocorrencia.idOcorrencia" ?
This is beeing tested in an Android device.
EDIT: Tested on Windows browser and the problem doesn't appear to happen.
Thank you
I guess you fill idOcorrencia on runtime? So the lag is producing this difference.
Try using a hash for the id or set it before running the SQL-query.

Categories

Resources