EJS recieve json containing code as string - javascript

Im sending a stringified json that should have property:
recievedObject: {"code":"this is a code snippet"}
(the code property has no specified programming language)
and then parsing it like this:
var items = JSON.parse('<%- JSON.stringify(items) %>')
When I try to parse the stringified json I received I get parsing errors (im guessing its because of the code string often containing characters like " ', line breaks etc.)
Is there a way to bypass or get around this problem without changing the string in any way? (I was able to get this working after encrypting it, but I would prefer not to do this)
example of how the code string might look:
code: "router.get('/', function(req, res, next) {\n" +
' try {\n' +
' const cities = spreadsheet.getData()\n' +
'\n' +
' } catch(err) {\n' +
' console.log(err)\n' +
' }\n' +
'\n' +
" res.render('index', { cities: cities})\n" +
'})'

Related

How to pass a question mark express router api to update sql

I want to update a varchar field (String) using and End-Point Api (Express NodeJS) but I have problem went I pass invalid inputs like question mark.
Express End-Point:
router.get("/updateField/:table/:field/:value/:num/:postid/", function(req, res) {
connection.query(
'UPDATE '+ req.params.table +' SET ' + req.params.field +' = '+JSON.stringify(req.params.value) +' where language ='+ req.params.num +' and post_id ='+req.params.postid
This code work fine:
http://localhost:3001/api/updateField/posts/TITLE/When/1/1
But this NOT WORK:
http://localhost:3001/api/updateField/posts/TITLE/When?/1/1
I send the request from react like this:
fetch(
"http://localhost:3001/api/updateField/" +
table +
"/" +
field +
"/" +
value +
"/" +
lenguage +
"/" +
post_id
);
Use javascript function encodeURIComponent() to escape special characters in URL parameters.
For example try this on your browser console and you'll get an idea:
console.log(
"http://localhost:3001/api/updateField/" +
table +
"/" +
field +
"/" +
encodeURIComponent(value) +
"/" +
lenguage +
"/" +
post_id
);
console.log(encodeURIComponent("When?"));
You will see that "When?" is replaced with "When%3F" in URL.
In Node.Js, you'll receive parameter value as string "When?".
To know more about encodeURIComponent(), refer to this

Two similar SQLite case statements, one works, the other does not

The first statement works, the second comes back with the error below. The strange thing is except for variable names, they are the same statement, so if one works, the other should work as well.
"Error: SQLITE_ERROR: no such column: RAF60"
Where RAF60 is my data. I thought that I may have a quote in the wrong place as it's trying to read my data as a column but can't see it, also looked for somewhere I may have swapped my data with an expression, but could not see that either.
The RFID1,2,3 and the CarPlates1,2,3 are formatted as text
Statement one exactly as it appears below.
msg.topic = 'SELECT "RFID1", "RFID2", "RFID3", case when "RFID1" = ' + readrfid + ' then RFID1 when "RFID2" = ' + readrfid + ' then RFID2 when "RFID3" = ' + readrfid + ' then RFID3 else null end dbrfid FROM SiteDB'
The output below from Node-Red, I reformatted it to make it easily readable. It shows that the data is populating the right fields and this code works. Its the above statement with the data inserted, just prior to being sent to SQLite
topic:
"SELECT "RFID1", "RFID2", "RFID3", case
when "RFID1" = 149544749819338 then RFID1
when "RFID2" = 149544749819338 then RFID2
when "RFID3" = 149544749819338 then RFID3
else null
end dbrfid FROM SiteDB"
payload: "149544749819338"
_msgid: "232263a1.02d34c"
_event: "node:ad044b82.8c419
0: object
Statement two, the sql query exactly as it appears below
msg.topic = 'SELECT "CarPlate1", "CarPlate2", "CarPlate3", "dbcarplate", case when "CarPlate1" = ' + readlpr + ' then CarPlate1 when "CarPlate2" = ' + readlpr + ' then CarPlate2 when "CarPlate3" = ' + readlpr + ' then CarPlate3 else null end dbcarplate FROM SiteDB
The output below from Node-Red, I reformatted it to make it easily readable. It also shows that the data is populating the right fields, but comes back with an error message, the statement below is the data just before its sent to SQLite
topic: "SELECT "CarPlate1", "CarPlate2", "CarPlate3", "dbcarplate", case
when "CarPlate1" = RAF60 then CarPlate1
when "CarPlate2" = RAF60 then CarPlate2
when "CarPlate3" = RAF60 then CarPlate3
else null
end dbcarplate FROM SiteDB"
payload: " RAF60 "
_msgid: "f2831f05.42c24"
_event: "node:61abb27f.bc684c"
It does not run in SQLite, and I get the error message.
"Error: SQLITE_ERROR: no such column: RAF60"

Why is the following way to assign a value to a JSON array not working?

I have this code:
compareList[productName] = productID + ',' + productHref;
console.log(productName + ' ' + productID + ' ' + productHref + ' ' + compareList.length);
Which logs into this (I have removed the link):
Acer Iconia B1-790 [NT.LDFEE.002] 112576 link removed for confidentiality 0
As you can see, all three variables are valid strings, but the json object still fails to assign (compareList.length logs as 0). I've been thinking and thinking but I simply can't figure it out. Any help is appreciated.
Maybe this version of adding and checking array length can be useful to you?
var compareList=[]
var productName = {productID:'saban',productHref:'http://saulic.com'};
compareList.push(productName);
console.log(compareList.length);

make Form Text URLEncoded format

Hollo,
I need to create a form text that sends the variable in URLEncoded format.
I need this for send SMS with API with this parameters (GET):
Username
APIKEY
Number
Text (URLEncoded)
How can I create this?
Thanks a lot for the collaboration :)
Assuming you are only missing the string generation part (and not the whole html + javascript stuff), you may have a function like :
function generateRequest(username, apikey, number, text) {
var baseUrl = "http://your.base.url/sms";
return baseUrl +
"?Username=" + username +
"&APIKEY=" + apikey +
"&Number=" + number +
"&Text=" + encodeURIComponent(text);
}
for more details about the encodeURIComponent, read this => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
I hope it will help you.

Send an array of objects from JS/TS (Angular 2) to WCF?

i have to send an array of objects like this
[{"Cod":"1"},{"Cod":"5"}]
to my C# WCF Service which i used until today without problems with wsHttpBinding (so no JSON using here, only XML).
The request, POST, contains this param:
let param = "<cod>" + data + "</cod>";
My problem is on WCF because i can't find a way to get that array of objects from the parameter of the method:
public string GetArrayOfObjects(CompositeType[] cod)
{//implementation not important because i can't enter here...}
where CompositeType is:
[DataContract]
public class CompositeType
{
string cod;
[DataMember]
public string Cod
{
get { return cod; }
set { cod = value; }
}
}
I have tried so many things like changing the parameter to:
Object[] cod
List<string> cod
...
Everytime i get this error (translating):
Exception generated by the formatter in an attempt to deserialize the message: Error trying to deserialize the parameter http://tempuri.org/:cod. InnerException: 'Error at line 1 position 341. Expected status 'Element' .. Found 'Text' with '' name space ''. '.
...
This is the post message that i'm sending:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<s:Header>" +
"<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService1/" + GetArrayOfObjects + "</a:Action>" +
"</s:Header>" +
"<s:Body>" +
"<" + GetArrayOfObjects + " xmlns=\"http://tempuri.org/\">" +
param +
"</" + GetArrayOfObjects + ">" +
"</s:Body>" +
"</s:Envelope>";
where param is defined before. The JSON.stringify of data inside param gives the first example of this question ([{"Cod":"1"},{"Cod":"5"}]). This post message was always fine with single value params.
Some sites show a solution with a json deserializer on wcf, but i'm using XML...it's not an option right now to re-implement the project in json.
I tried for one day but i didn't find a solution, i'd love to hear some solutions from you! Thanks to all.

Categories