setQueryParameters error parameters format - ibm-mobilefirst

I have this ionic service but when i pass the parameters in set Queryparametrs function it wont work.
var sample = function(title,description,adress,country,userid)
{
var req = new WLResourceRequest("/adapters/eventAdapter/addEvent", WLResourceRequest.POST);
req.setQueryParameters("params", "['"+title+","+description+","+adress+","+country+","+userid+"']");
return req.send().then(function(res) {
........
}, function(bad) {
.......
});
}
any help ?

If your content (title, description, etc) contains any invalid json characters, it could be that the string you generated is invalid.
As you guessed correctly, using JSON.stringify is the safer option.
var params = [title, description, adress, country, state, userid];
req.setQueryParameters("params",JSON.stringify(params));
Also, your request is using POST, and so it is expected to use form parameters instead of query parameters:
req.sendFormParameters({"params":JSON.stringify(params)})

Related

How to build query as variable (from user Input) in prisma.queryRaw without using queryRawUnsafe

I was trying to query my (postgres) db with a customizable statement built front end.
My resolver gets the built query inside the input param, but when I use the queryRaw method I get this error:
`"\nInvalid `prisma.queryRaw()` invocation:\n\n\n Raw query failed. Code: `42601`. Message: `db error: ERROR: syntax error at or near \"$1\"`"`
Is there a way to build a custom query and pass it like the input variable WITHOUT USING queryRawUnsafe to prisma? (queryRawUnsafe works fine, but well.. it's unsafe XD) Thanks <3
Here is my code.
getCars: (_parent, { input }, { prisma }) => {
if(input){
console.log(input) // --> SELECT * FROM car WHERE car."plate" ILIKE '%123%' //type String
const differentInput = '%123%'
// const result = prisma.$queryRaw`SELECT * FROM car WHERE car."plate" ILIKE '%123%'` // works
// const result = prisma.$queryRaw`SELECT * FROM car WHERE car."plate" ILIKE ${differentInput}` // works
// const result = prisma.$queryRawUnsafe(input) // works
const result = prisma.$queryRaw`${input}` // Doesn`t work
return result
}
// ... Other code
}
prisma.$queryRaw only accepts templated strings, not just strings. You can use the Prisma.sql helper to generate those templated strings to get the expected results. That might look like:
const sql = Prisma.sql`SELECT * FROM car WHERE car."plate" ILIKE '%123%'`
const result = prisma.$queryRaw`${sql}`
The queryRaw documentation mentions Prisma.sql with other examples but doesn't show any examples of what you are trying to do.

What is the equivalent of keccak256 in solidity?

I am going to get the same value that is produced by keccak256 in solidity.
This is the code in my solidity file and I want to get the same value in the javascript file using ethers or web3.
bytes32 node = keccak256(abi.encodePacked(nodeString));
I got the same value of abi.encodePacked(nodeString)) by using ethers.utils.solidityPack.
const abiEncodedPackedString = ethers.utils.solidityPack(['string'], [nodeString]);
But when I tried ethers.utils.solidityKeccak256, the result wasn't the same as node in solidity.
const nodeInJavascript = ethers.utils.solidityKeccak256(['string], [abiEncodePackedString]);
I have also tried ethers.utils.keccak256(abiEncodePackedString) but I couldn't get the result either.
in web3 I used this and worked:
export const createCourseHash = (courseId, account) => {
const hexCourseId = web3.utils.utf8ToHex(courseId)
const courseHash = web3.utils.soliditySha3(
{ type: "bytes16", value: hexCourseId },
{ type: "address", value: account }
)
return courseHash
}
in ethers, you are passing plain string. try to convert it to hex value. in fact if you check the documentation, they are not passing plain strings:
The value MUST be data, such as:
an Array of numbers
a data hex string (e.g. "0x1234")
a Uint8Array

How can I save part of a string in an alias using Cypress?

I'm trying to save just a number from a string I get from a paragraph but when I try to asign an alias to it and then check the value it returns undefined. I've tried a few solutions I found but none of those seem to work for me. These are two ways I tried (I tried another one similar to the second one but using split, had same result). The console.log inside of the 'then' doesn't show in the console, and when I try the alias after the code is when I get undefined.
cy.get('p')
.eq(1)
.should('have.text', '/[0-9]+/g')
.as('solNumber')
cy.get('p')
.eq(1)
.invoke('text')
.then((text)=>{
var fullText = text;
var pattern = /[0-9]+/g;
var number = fullText.match(pattern);
console.log(number);
})
.as('solNumber')
Please convert with + operator and return the numeric value if you want numeric type to be stored.
cy.get('p').eq(1)
.invoke('text')
.then(fullText => {
const number = fullText.match(/[0-9]+/);
return +number // text to numeric
})
.as('solNumber')
cy.get('#solNumber')
.should('eq', 42) // numeric type
});
Running your 2nd code on this,
<p>21</p>
<p>42</p>
gives the correct outcome
cy.get('p')
.eq(1)
.invoke('text')
.then((text)=>{
var fullText = text;
var pattern = /[0-9]+/g;
var number = fullText.match(pattern);
console.log(number); // logs 42
})
.as('solNumber')
cy.get('#solNumber')
.should('eq', '42') // passes
So, you need to inspect the DOM, it looks like it's not what you expect.
The first attempt you were passing a jquery element to the .should() and although some chainers change the subject yours did not so it saved the jquery element as solNumber.
The second attempt invokes the .text() which was passed to the .then() it logs the number correctly. However, you did not return anything at the end of the .then() block, therefore, solNumber should hold the entire paragraph.
This should help you out to extract the specific number and save it as an alias.
cy.get('p')
.invoke('text')
.invoke('trim')
.then(paragraph => {
const matcher = /some/
expect(paragraph).to.match(matcher) // check number is there
const indexOfText = paragraph.match(matcher) // get index of match text
return paragraph.substring(indexOfText.index, indexOfText.index + indexOfText[0].length) // return substring
})
.as('savedText')
cy.get('#savedText')
.then(cy.log) // will print out the number you seek

akka-http: How to extract list of query paramerters

In akka-http, how do we extract a list of query parameters of varying length from incoming request?
Request url can be like this:
.../employees?city=london,ny,paris
Number of cities may vary with every request.
From your solution, you can replace the Symbol part like
parameters("city".repeated)
See the akka doc
If you want to keep your value as a comma-separated list of values, you can create a custom directive like
def paramAsList(key: String): Directive1[List[String]] =
parameter(key)
.map(x => x.split(",").toList)
...
get {
paramAsList("city") => cities {
....
With this, your url .../employees?city=london,ny,paris should work
Got it working as:
path("searchByCity") {
get {
parameters(Symbol("city").*) {cities =>
.....
}
}
}
URL is now as:
.../employees?city=london&city=ny&city=paris
function getQueryParams(url){
let urlParts = url.split('?');
if(urlsParts?.length > 1){
let params = urlParts[1].split('&');
return params
}
return null
}
var queryParams = getQueryParams('.../employees?city=london,ny,paris')

"update" query - error invalid input synatx for integer: "{39}" - postgresql

I'm using node js 0.10.12 to perform querys to postgreSQL 9.1.
I get the error error invalid input synatx for integer: "{39}" (39 is an example number) when I try to perform an update query
I cannot see what is going wrong. Any advise?
Here is my code (snippets) in the front-end
//this is global
var gid=0;
//set websockets to search - works fine
var sd = new WebSocket("ws://localhost:0000");
sd.onmessage = function (evt)
{
//get data, parse it, because there is more than one vars, pass id to gid
var received_msg = evt.data;
var packet = JSON.parse(received_msg);
var tid = packet['tid'];
gid=tid;
}
//when user clicks button, set websockets to send id and other data, to perform update query
var sa = new WebSocket("ws://localhost:0000");
sa.onopen = function(){
sa.send(JSON.stringify({
command:'typesave',
indi:gid,
name:document.getElementById("typename").value,
}));
sa.onmessage = function (evt) {
alert("Saved");
sa.close;
gid=0;//make gid 0 again, for re-use
}
And the back -end (query)
var query=client.query("UPDATE type SET t_name=$1,t_color=$2 WHERE t_id = $3 ",[name, color, indi])
query.on("row", function (row, result) {
result.addRow(row);
});
query.on("end", function (result) {
connection.send("o");
client.end();
});
Why this not work and the number does not get recognized?
Thanks in advance
As one would expect from the initial problem, your database driver is sending in an integer array of one member into a field for an integer. PostgreSQL rightly rejects the data and return an error. '{39}' in PostgreSQL terms is exactly equivalent to ARRAY[39] using an array constructor and [39] in JSON.
Now, obviously you can just change your query call to pull the first item out of the JSON array. and send that instead of the whole array, but I would be worried about what happens if things change and you get multiple values. You may want to look at separating that logic out for this data structure.