I’m trying to get the total number of records in a wix collection using the code show below:
wixData.query("Client").count().then((num) => {
clientTableIndex = num;
}).catch((error) => {
let errorMsg = error.message;
let code = error.code;
console.log("@Public IndexOf().");
console.log(code + ": " + errorMsg);
});
But for some reason its not giving me the count, neither it is showing up any error. I also tried using the find()
like so:
wixData.query("Client")
.find()
.then((num) => {
clientTableIndex = num.totalCount;
}).catch((error) => {
let errorMsg = error.message;
let code = error.code;
console.log(code + ": " + errorMsg);
});
console.log(clientTableIndex);
let insertToClient = {
"memberId": wixUsers.currentUser.id,
"title": "Client0".concat(clientTableIndex + 1)
But still no luck. I’ve given full permission to the Collection Client
in wix database. See below:
I’m kind of stuck at this moment. I’m sure, something I’m missing, which causing this issue.
Any help will be greatly appreciated.
Hi Jilu Thomas
try “num.items.length”
thats what i use in my code
greets
Kristof.
Nope, still not working. I tried that too.
@jiluthomas
then there is 1 more thing to do i think
doing a query takes a small amount of time,
in that time your code doesn’t stop.
if you would add a line console.log(clientTableIndex) right under where you define it it might work.
if you want to use it behind the .then function u could do
async function nameofthefunction(){
await wixData.query …
see where i added the text “async” and “await”
here is some more info about it
https://support.wix.com/en/article/corvid-working-with-promises
Kristof.
@volkaertskristof Thank you so much! Now I got the code working and I also understood what I was doing wrong. I was actually performing the task outside the .then function. After placing the code within .then function things starting working as expected.
You saved my day.
Cheers.
@jiluthomas
I’m happy evrything worked out!
and yea i had some problems with working outside the .then function awhile ago to.
working with async and await helps.
if you do a query and need the result only later then there is no problem.
but if you need the re sult straight away you will need to work in the .then or with an async/await .
have a nice day!
kristof.
@volkaertskristof Yeah sure, will keep that in mind. Learned something new today.
Awesome!