Converting a PostgresSQL query into Knex.js - javascript

I have to use the LOWER function in whereIn using knex.
I want to change the below line of code
whereIn("name", members);
If I do whereIn('LOWER("name")', members);, getting error as
Column LOWER("name") is not present.
I tried to use knex.raw but again not getting the correct syntax.
const roleMembers = await t("roles")
.select({ role_id: "id" }, "name")
.whereIn(t.raw("LOWER('name')"), members);
Error:
index.d.ts(1184, 5)
When I try to add await, syntax error goes away but when I execute this code I get the below error
error: LOWER(name) - syntax error at or near "LOWER"
I'm referring http://knexjs.org/#Builder-whereIn
We have whereRaw, Do we have a similar thing for whereIn? Need some help to fix this issue.
Note: I used all LOWER, lower, Lower.

knex.raw() can be used for the column component
knex('members').whereIn(knex.raw('Lower("name")'), members)
select * from `members` where Lower("name") in ('Tom', 'Dick', 'harry')

The final solution is below.
const roleMembers = await t("roles")
.select({ role_id: "id" }, "name")
.whereIn(t.raw('LOWER("name")') as any, members);
My changes to the original code are
Quotes: swapped " and '
lower, Lower and LOWER worked
Typescript error: added as any
Thanks Matt and Mikael for guidance.

Related

deleteEventAsync Expo Calendar Not Reading recurringEventOptions

I'm having a strange error when it comes to deleting events using Expo Calendar. I've used the updateEventAsync which works perfectly and has very similar syntax so I'm unsure why my deleteEventAsync function isn't working.
I'm passing two parameters into this function, 'future' is either true or false, and 'start' is the date of the event being deleted (or the first if recurring events are being deleted). Start is a Date object and is worked out in the exact same way as my updateEventAsync so it's not a Type Error I think.
Finding the event is also not the issue, as if I comment out the recurringEventOptions, this single event will be deleted. Therefore I believe that there's something wrong with the syntax of the recurringEventOptions somehow even though all the examples I've found look like this.
console.log("Found Match", allEvents[i].startDate);
console.log(future, " | ", start);
await Cal.deleteEventAsync(allEvents[i].id,
{
futureEvents: future,
instanceStartDate: start,
}
);
This is the output:
"Found Match 2022-11-08T01:40:22.000Z
true | 2022-11-08T01:40:22.000Z
An exception was thrown while calling `ExpoCalendar.deleteEventAsync` with arguments `(
{
id = "99B66178-1CAE-447A-A93E-E90AC11EB4FF";
instanceStartDate = {
};
},
{
futureEvents = 1;
}
)`: -[__NSDictionaryM __baseAttributedString]: unrecognized selector sent to instance 0x6000019a9d00
at node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:104:50 in promiseMethodWrapper
at node_modules/expo-modules-core/build/NativeModulesProxy.native.js:27:27 in moduleName.methodInfo.name
at node_modules/expo-calendar/build/Calendar.js:248:11 in deleteEventAsync
at node_modules/expo-calendar/build/Calendar.js:240:7 in deleteEventAsync"
From the logs, it's clear that the dates match, and the error is showing that it has read futureEvents as true (1). I can't work out why it doesn't like instanceStartDate.
Would love some help!

Why do I get an "Unexpected character '#'" parsing error when I run ESLint?

I'm getting this parsing error (i.e., error Unexpected character '#' ParseError) returned when running eslint --cache --fix. I'm not sure why I'm seeing this error though, since I don't have an '#' anywhere near that line that's being flagged as problematic. The following is a snippet of my code where the issue is happening:
const getCustomerInformation = async (id, role) => {
return await gql
.query({
query: GET_CUSTOMER_DATA,
variables: {
id
}
})
.then((res) => {
response[role] = res
})
}
GET_CUSTOMER_DATA is being imported from another file and is as follows:
export const GET_CUSTOMER_DATA = gql`
query getCustomerInformation($id: String!) {
OSDTGetCustomerInformation(id: $id) {
... on CustomerSuccess {
id
fullName
}
... on CustomerError {
message
}
}
}
`
The line that's being flagged as problematic is query: GET_CUSTOMER_DATA at character 42 in the 1st code block. As mentioned earlier though, I don't see an instance of '#' being used, so I'm not sure why there's a linting issue. I'd really appreciate it if someone could help me understand what could be the issue here.
FWIW, I'm using ESLint v7.31.0.
It turns out that the pre-commit linter was recognizing that I hadn't added any documentation for getCustomerInformation(). So, the linter attempted to solve this by adding a comment with getCustomerInformation()'s parameters (i.e., id & role) that included '#'. However, the linter wasn't adding them in the right place — which is what was throwing the parsing error. So, I had to manually add documentation. I added the following above where getCustomerInformation() is being defined:
/**
* #param {string} id
* #param {string} role
*/

How to perform chai assert to verify that the table is empty

I am working on protractor using cucumber. The scenario is that data is displayed in a table, and after deleting it, I have to verify that the table is blank. Following is the structure of json:
I tried the following, but it gives error:
Then('The user should be deleted', async function () {
return await expect(element.all(by.xpath("//table[#class='table table-bordered table-striped']")).count()).to(0);
});
I get the following error:
TypeError: expect(...).to is not a function
Any pointer will be quite helpful.
Not sure, if this is relevant to this Post. But here is my 2 cents.
jsonPayload.json:
[
{
...
...
},
{
...
...
}
]
For Example, if i have above JSON payload & if i need to assert that the payload is empty :
expect(Object.keys(jsonPayload).length).to.equal(0)
Try similar to this in your case & see if it works.
Ensure that your Xpath match is returning the array & you are are able to get the count of it.

How to resolve TypeError: Cannot convert "null" to int

Error:
TypeError: Cannot convert "null" to int.
function play(connection, message){
var server = servers[message.guild.id];
server.dispatcher = connection.play(ytdl(server.queue[0], {filter: "audioonly"}));
server.queue.shift();
server.dispatcher.on("end", function(){
if(server.queue[0]){
play(connection, message);
}else {
connection.disconnect();
}
})
}
The error message I'm getting from the tester is:
TypeError: Cannot convert "null" to int
TypeError: Cannot convert "null" to int
Error: "abort(TypeError: Cannot convert "null" to int). Build with -s ASSERTIONS=1 for more info."
at (anonymous) (file:///c:/Users/pc/Documents/Discord%20Bot/node_modules/opusscript/build/opusscript_native_wasm.js:8:1741)
at emit (events.js:311:19)
I'm begging you, please help me.
Thanks.
I may have a solution for your problem!
UPDATE:
Following this answer and my bug report, prism-media released a fixed version. Thank you for such a quick solution! Keeping the text below just as a reference.
TL;DR
Find Opus.js in node_modules folder and go to line 55. You should see the following:
return this.encoder.encode(buffer, Opus.name === 'opusscript' ? null : this._options.frameSize);
CHANGE IT TO:
return this.encoder.encode(buffer, this._options.frameSize);
Then delete the same part at line 59.
Explanation
The exception is thrown in toWireType in opusscript_native_wasm.js which expects its second argument to be an int, but it gets a null. Hence, the exception.
Tracing it back, toWireType is called by OpusScriptHandler.encode for all its arguments.
Those four arguments come from OpusScript.prototype.encode in opusscript.js:69. The last argument causing the trouble (being null) is `frameSize.
The encode function is called from Opus.js:55 in prism-media which is a different module.
return this.encoder.encode(buffer, Opus.name === 'opusscript' ? null : this._options.frameSize);
There it is, the null causing all the trouble. So the exception is in fact not a fault of the opusscript package.
Long-term solutions
I reported the bug to the module owner, hopefully it gets fixed. Update: it has been fixed within a few hours! :)
Alternative solution
Using a custom loader should also work fine. It's still a hack, but more persistent :)

Query on pointer to same class in parse

I have a subclass called TreeFolderObject. One of the column headings is called "parent" and is a pointer field. It points to other another object in the same class. Here is the stripped down code I am using to attempt to read data from this structure:
var TreeFolderObject = Parse.Object.extend('TreeFolderObject');
var folderQuery = new Parse.Query(TreeFolderObject);
folderQuery.include("parent");
folderQuery.find().then(function(results) {
for (i in results) {
treeData.push({
title: results[i].get('folderName'),
objectId: results[i].id,
parent: results[i].get("parent")
});
console.log(results[i].get("parent").get("folderName") );
}
},
function(error) {
console.log("failed, with error code: " + error);
}
);
The console line reports the following output:
Uncaught TypeError: Cannot read property 'get' of undefined
I have also tried shortening the console.log line to just:
console.log(results[i].get("parent") );
and this reports the following:
undefined
index.html:100
ParseObjectSubclass {className: "TreeFolderObject", _objCount: 32, id: "oq5o2zFqIM"}
index.html:100 ParseObjectSubclass {className: "TreeFolderObject", _objCount: 24, id: "oq5o2zFqIM"}
index.html:100 ParseObjectSubclass {className: "TreeFolderObject", _objCount: 41, id: "oq5o2zFqIM"}
etc
This indicates that the browser client is obtaining the information I am after. I just can't work out how to get the data "out", if that makes sense.
Ultimately, I want to identify the parent object of each object so I can iterate through the class.
What I am doing wrong? I have tried searching here and on the web in general and there are a lot of very similar questions. This is obviously a confusing topic. I just can't work it out though and any help would be much appreciated. How do I get the console to print out the folderName property of the parent object?
Here's a screenshot of the parse control panel, if that helps:
enter image description here
Add "var" in front of i like this
var TreeFolderObject = Parse.Object.extend('TreeFolderObject');
var folderQuery = new Parse.Query(TreeFolderObject);
folderQuery.include("parent");
folderQuery.find().then(function(results) {
for (var i in results) {
treeData.push({
title: results[i].get('folderName'),
objectId: results[i].id,
parent: results[i].get("parent")
});
console.log(results[i].get("parent").get("folderName") );
}
},
function(error) {
console.log("failed, with error code: " + error);
}
);
Answering my own question here:
I eventually discovered the answer, and its got nothing to do with labeling i as a var, or the fact that I'm querying the same class. The solution is to put a condition line in the query. In other words, insert this after the first two lines of the code I posted earlier:
folderQuery.exists("parent");
In my case every object in the class, apart from one, has a parent, so this works fine. What I don't understand is why it should be necessary to put a condition on the query at all, if I'm intending to query the entire class.
I discovered the solution after browsing lots of parse pointer related questions on StackOverflow. No one else seems to have fallen into the exact same trap of leaving out the condition line, and eventually I realised that my parse query wasn't following the convention. If this realisation helps anyone else out there, I will be happy to have helped!
If anyone can help with an explanation as to why I need to put a condition line in, even though I'm querying the entire class, please let me know. In the meantime, I'm happy to have found this solution as it was holding up development of my project.

Categories

Resources