this is my first post so bare with me please.
We are currently working on our first Web-Project and have our problems finding the issues within our project. The project we are working on is a simple website for students to see which groups and modules they have joined. One example for a page is the group-overview which display one group with its members and some other data. The content of the member-table should be filled dynamically based on the group you currently selected. We tried to get this information within the function to fill the table by simply calling "request.query" but the received object is empty.
I´ll leave the code below. Sorry in advance if I have missed any necessary information - feel free to contact me.
Client-Side Fetch:
fetch('/getStudentsIntoTable')
.then (response =>
{
console.log(response);
return response.text();
}).then (text =>
{
document.getElementById("tableStudentGroup").innerHTML = text;
})
Server-Side Get-Request:
app.get("/getStudentsIntoTable", (request, response, next) => {
console.log(request.query); <!-- This hands back "{}" -->
let userID = request.session.userId;
let abfrage = "SELECT * FROM User ORDER BY 'Nachname'";
connection.query(abfrage, function(err, result, fields)
{
if (err) response.send("Es konnten keine Daten abgerufen werden.");
if (result != null)
{
var resultString = "";
for (var i = 0; i < result.length; i++)
{
resultString += "<tr><td>" + result[i].User_ID + "</td>" + "<td>" + result[i].Vorname + " " + result[i].Nachname + "</td>" + "<td>" + result[i].E_Mail + "</td></tr>";
}
response.send(resultString);
}
});
});
The URL Looks like this:
"http://localhost:3000/Gruppe/?Gravelshipping++"
The structure of a query string is a ? followed by a series of key=value pairs, separated by & characters (and with the keys and values URL encoded).
For example:
?name1=value1&name2=value2
Your URL doesn't follow that format.
I have an application that uses Javascript to parses a XML array that is returned from a Webservice and iterates through it and builds it into a table body. It has been working with no issues until lately.
We had some changes on the database that the Webservice is returning results from in which now there are a few columns that could potentially have blanks or null values.
The Javascript fails to run when it hits a childNode that has a blank or null value.
Below is a snapshot of the browser error:
So my question is how do I handle those blanks so that the Javascript will just build an empty string into the table body and continue iterating through the xml array?
I have tried to build an if statement into the Javascript in the for loop to replace the blank or null value with '', but I'm not sure it's going to be doable with the way my table body is being built.
for (i = 0; i < x.length; i++) {
tbody += "<tr><td class=col1>" +
x[i].getElementsByTagName("CheckInDate")[0].childNodes[0].nodeValue +
"</td><td class=col2>" +
x[i].getElementsByTagName("CheckOutDate")[0].childNodes[0].nodeValue +
"</td><td class=col3>" +
x[i].getElementsByTagName("CheckInOut")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("address")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("names")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("companyName")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue.substr +
"</td><td>" +
x[i].getElementsByTagName("contactPhoneNum")[0].childNodes[0].nodeValue +
"</td ></tr >";
}
Being as how this is an existing application I don't want to rebuild all the functions that build tables using this method so I hope there is an easy solution to this that I'm not seeing.
The problem is that .childNodes will be null if the XML element doesn't have any content (any text nodes). You seem to have a lot of repeating code, to fix this you can create a function to get the content of an XML node with a specific tag name if it has any content or return an empty string.
Here is an example:
function getElementContent(element, tagName) {
const e = element.getElementsByTagName(tagName);
if (e && e.childNodes && e.childNodes.length) {
return e[0].childNodes[0].nodeValue
}
return '';
}
const tbody = x.map(e => `<tr><td class=col1>${getElementContent(e, 'CheckInDate')}</td><td class=col2>${getElementContent(e, 'CheckInDate')}..... `).join('');
We are working on an ubuntu server, where we have installed node-red. What we want is to take data from one table on our MySQL Database, and then move it to another table.
Our flow looks like this:
Our 'Select Tolerance' node contains:
msg.topic = "SELECT * FROM Tolerance";
return msg;
Simple code, which selects data from our database. If we connect a debug node like this:
We then see an output looking like this:
We want to pick all data, so we need to go through all objects in the array and make sure to take all values and send them over to our new database table. We're using the 'New Tolerance' node to do so, and 'New Tolerance' contains:
var i;
var secured = 1;
for (i = 0; i < msg.payload.length; i++) {
var time_start = msg.payload[i].time_start
var time_end = msg.payload[i].time_end
var temperatur_lpn = msg.payload[i].temperatur_lpn
var temperatur_rising = msg.payload[i].temperatur_rising
var temperatur_weather = msg.payload[i].temperatur_weather
var temp_compare_WL = msg.payload[i].temp_compare_WL
var temp_compare_WR = msg.payload[i].temp_compare_WR
var out = "INSERT INTO Sunrise_Tolerance (time_start, time_end, temperatur_lpn, temperatur_rising, temperatur_weather, temp_compare_WL, temp_compare_WR, secured)"
out = out + " VALUES ('" + time_start + "','" + time_end + "','" + temperatur_lpn + "','" + temperatur_rising + "','" + temperatur_weather + "','" + temp_compare_WL + "','" + temp_compare_WR + "','" + secured + "');"
msg.topic = out;
return msg;
}
The problem is that we only receive the first row of data (first object in the array) and not the rest. Can anyone figure out why we dont receive all data, but only the first?
The problem comes the fact you have a return statement inside the for loop.
That will exit the function the first time it reaches that point - so your loop never loops.
If you want to send multiple messages you should use node.send(msg); in your for loop. The only thing to watch out for is if you call node.send with the same object multiple times, as the message is passed by reference, you would get some odd side-effects. As you only care about msg.topic in this instance, you can afford to create a new message object each time.
So, instead of the return msg statement, you could do:
for (i ... ) {
var out = "INSERT ...";
...
node.send({topic: out});
}
// Return without any arguments so no further messages are sent.
return;
I have a function that I need to use for filtering table rows:
setFilterString("Filter");
But I have a problem. I can set it to
setFilterString("OrderID = 5");
and it will filter out row where OrderID is equal to 5 but if i try using a variable that has a value taken before like this
setFilterString("OrderID = vOrderID");
I get error "Invalid column name 'vOrderID'." (as vOrderID is variable and not a column, I guess)
I have seen somewhere in filter section inputting something like this ("OrderID = '" & vOrderID & "'") but it doesn't have any result at all for me. Doesn't even throw any error in the console.
JavaScript assumes you are just passing a string to the function. If you want to use the variable, you should try this:
setFilterString("OrderID = '" + vOrderID + "'"); // Results in OrderID = '5'
or
setFilterString("OrderID = " + vOrderID); // Results in OrderID = 5
depending on the body of your function.
Use + instead of &: setFilterString("OrderID = " + vOrderID) should work.
Use "+" for merge strings:
setFilterString("OrderID = " + vOrderID)
You can also try to use ${idvOrderID} inside string:
setFilterString("OrderID = ${vOrderID}")
Or:
setFilterString(sprintf("OrderID = %s", vOrderID))
Remember about difference between ' and "
i'm doing an application with ajax using jQuery and some other tools, and in some part i want to retrieve data with ajax using a classic ASP backend, i saw that exists a good implementation of a JSON class in AXE (Asp extreme edition) framework, and i used it but currently i don't understand how to use it well.
Edit: based on the correct answer of JSON.Stringify fails on Scripting.Dictionary objects Thread, i decided to make a custom function to process Recordsets.
Edit 2: Now i'm losing the value data when call JSON.stringify inside function JSONStringify(object).
when the Recordset is passed as value to JSONStringify everything is ok but when JSON.stringify is executed, the "value" parameter that must contain the recordset becomes undefined
What i'm expecting (example)
passing a Recordset with from a SQL query SELECT name, tel FROM users a see an output like this
[
{"name":"Jonh Smith", "tel":"12345678"},
{"name":"April Michelson", "tel":"77788802"},
...
]
passing a Dictionary and see something similar based in the elements declared in dictionary.
{
"element1":"value1",
"element2":"value2",
"element3":"value3",
"element4":"value4",
"element5":"value5"
}
and if i like to support other type object i can do it expanding the function
Source Code
getcatalogos.asp
<!--#include file="../includes/conexion.asp" -->
<!--#include file="../includes/json2.asp" -->
<!--#include file="../includes/json-stringify-parser.asp" -->
<%
Response.ContentType = "application/json"
dim aVals(2)
function getCatalogo(tipo, params)
Dim oConn,oCmd,sSQL,oRs,cont2
Dim aData,oPar,cont
dim Info
set oConn = Server.CreateObject("ADODB.Connection")
set oCmd = Server.CreateObject("ADODB.Command")
sWhere = ""
oConn.ConnectionString = strcon
oConn.Open
Set oCmd.ActiveConnection = oConn
select case tipo
case "g"
sSQL = " SELECT cve_gr, descr FROM gr ORDER BY descr ;"
case "z"
sSQL = " SELECT cve_zn, descr FROM zn WHERE cve_gr = ? ORDER BY descr ;"
if IsArray(params) Then
Set oPar=oCmd.CreateParameter (params(0),129,1,2,params(1))
oCmd.Parameters.Append(oPar)
End if
case else
getCatalogo = false
exit function
end select
oCmd.CommandText = sSQL
Set oRs = oCmd.Execute()
if Not oRs.EOF Then
response.write(JSONStringify(oRs))
getCatalogo = true
else
getCatalogo = false
end if
oConn.Close
end function
aVals(0) = "cve_gr"
aVals(1) = request.querystring("gr")
if Not getCatalogo(request.querystring("t"),aVals) Then
%>error<%
end if
%>
json-stringify-parser.asp
<!--#include file="vbsTyper.asp" -->
<script runat="server" language="JScript">
function JSONStringify(object) {
VBSTypeName(object);
return JSON.stringify(object,stringifyData);
}
function stringifyData(holder, key, value) {
var sType = '';
var result;
//response.write('pre...holder=' + holder + ',key=' + key + ',value=' + value);
sType = VBSTypeName(value);
//response.write('post =' + sType);
//response.write(sType);
switch(sType){
case 'Dictionary':
result = '{';
for(var enr = new Enumerator(value); !enr.atEnd(); enr.moveNext()){
key = enr.item();
result += '"' + key + '": ' + JSON.stringify(value.Item(key));
};
result += '}';
return(result);
break;
case 'Recordset':
response.write('here!!!');
var sTemp = '';
result = '{';
while(!value.EOF){
if(Len(result) > 0){
result += ',';
}
result += '{';
for (var i = value.Fields.Count - 1; i >= 0; i--){
if(len(sTemp) > 0){
sTemp += ',';
}
sTemp += '"' + value.Fields(i).name + '":' + JSON.stringify( value.Fields(i).value);
};
result += '}';
}
result += '}';
return result;
break;
default:
//response.write(sType);
return(value);
}
// return the value to let it be processed in the usual way
return result;
}
</script>
vbsTyper.asp
<%
Function VBSTypeName(Obj)
dim sType
sType = Cstr(TypeName(Obj))
response.write(sType)
VBSTypeName = sType
End Function
%>
This:
response.write(JSON.stringify(oRs))
Should read something like this:
Do Until oRS.EOF
response.write(JSON.stringify(oRs("cve_gr") & ":" & oRs("descr"))
oRS.MoveNext
Loop
kind of achieve it...
Short version: i had to modify the json2.asp and hack the stringify() function definition to make it works.
Long Version
later of see every line of code and giving up on the problem. i decided to take a look into json2.asp (AXE Framework) and try to see what it happening there.
look what i see:
from the lines 682 to 687 theres a validation if a custom stringy parser is found but later doesn't do anything ... only returns the Object value.
there's speculation from here, because i don't understand well how works every Javascript implementation, but implying that the original code runs well, that a standart javascript interpreter (read everything else but microsoft here) will force a serialization of the object in this sutuation, but the JScript interpreter try to parse the object and pass the value as null. resulting in that every function that uses a custom stringyfier can't read the object passed in the function.
what i did to resolve ok i inserted this chunk of code before line 688, forcing to execute the custom stringyfier with value directly passed as argument avoiding the implicit parsing.
// Hack & patch to deliver the stringify-ing of the object correctly
// IDK if this is CORRECT or dont but it works in VbScript
if(replacer){
var textval = rep(this,'',value);
value = textval;
}
later i had to do some changes in json-stringify-parser.asp because i had to fix some bugs resulting in this code
<!--#include file="vbsTyper.asp" -->
<script runat="server" language="JScript">
function JSONStringify(object) {
//VBSTypeName(object);
return JSON.stringify(object,stringifyData,4);
}
function stringifyData(holder, key, value) {
var sType = '';
var result;
//response.write('pre...holder=' + holder + ',key=' + key + ',value=' + value);
sType = VBSTypeName(value);
//response.write('post =' + sType);
//response.write(sType);
switch(sType){
case 'Dictionary':
result = '{';
for(var enr = new Enumerator(value); !enr.atEnd(); enr.moveNext()){
key = enr.item();
result += '"' + key + '": ' + JSON.stringify(value.Item(key));
};
result += '}';
return(result);
break;
case 'Recordset':
//response.write('here!!!');
var sTemp;
result = '';
while(!value.EOF){
if(result.length > 0){
result += ',';
}
result += '{';
sTemp=''
for (var i = 0; i < value.fields.Count; i++){
if(sTemp.length > 0){
sTemp += ',';
}
//response.write("i=" + i + ",");
sTemp += '"' + value.fields.item(i).name + '":' + JSON.stringify( value.fields.item(i).value);
};
result += sTemp + '}';
value.moveNext();
}
result = '{' + result + '}';
return result;
break;
default:
//response.write(sType);
return(value);
}
// return the value to let it be processed in the usual way
return result;
}
</script>
the part to parse a Recordset works, the part to parse a dictionary is same like the shown in JSON.Stringify fails on Scripting.Dictionary objects (a.k.a. i haven't tested yet) but for now i'm done with this.
testing my changes with a recordset object results in this output
"{
{\"clave\":\"BC\",\"descripcion\":\"Cal\"},
{\"clave\":\"CT\",\"descripcion\":\"Center\"},
{\"clave\":\"NE\",\"descripcion\":\"Norw\"},
{\"clave\":\"NO\",\"descripcion\":\"Nore\"},
{\"clave\":\"NT\",\"descripcion\":\"North\"},
{\"clave\":\"OC\",\"descripcion\":\"East\"},
{\"clave\":\"OR\",\"descripcion\":\"West\"},
{\"clave\":\"PE\",\"descripcion\":\"Pen\"},
{\"clave\":\"SE\",\"descripcion\":\"Southe\"},
{\"clave\":\"ZM\",\"descripcion\":\"Met\"}
}"
Questions Left
question that i have left withis kind of hack.
it's Ok that the output has (") character at the beggining and the
end and i everything else has escape sencuences ?? or it's something
that should not occurs.
this kind of hack is really wrong ... i can do it better, in this
kind of situation ?
it's something wrong in my conclusion or arguments ??