"truncate" not working [SOLVED]

Hello together,
i have a little problem with the truncate-command. What do i wrong?

function delete_Data (parameter) {console.log("DELETE-DATA")
    wixData.truncate("Tarif", {suppressAuth:true})
    .then( () => {
          $w('#dataset1').onReady(()=>{
            $w('#dataset1').refresh()
          })
        console.log("All items removed");
    })
    .catch( (err) => {
 let errorMsg = err;
    });
}

Database: “Tarif” (all permissions)

Is this in the backend? The suppressAuth options only works in BE. However, since you are trying to use a dataset, you must be in the frontend.

Also, it’s not the best practice to mix WixData queries together with datasets. Although I don’t know what you’re trying to do. I can guess that you want a Repeater refreshed. Is that it?

Hello Yisrael,

yes i work just in frontend, never have been before in backend :grin:, but soon…
Yes, i want just to delete completely all data in my collection (“Tarif”) and refresh a table… take a look here…
https://www.wix.com/corvid/forum/community-discussion/textinput-and-number/p-1/dl-5ef3d0d80856a10034305ba1?origin=member_comments_page

There was a discussion about Numbers/Strings and i found this “parseFloat”, so i decided to inspect it a little bit more and do a little [How2], but as you can see, now having problems myself xD​:grin: (my way of learning) :sweat_smile::laughing:

In addition to what Yisrael mentioned in his answer, the dataset’s onReady() function needs to be inside the page’s $w.onReady() function.

And to use the suppressAuth and suppressHooks , you need to include them on the backend .

Hello Ahmad,
ok i undestand. I think i don’t even need this (supressHook) because my Database is opened with full access. (It was just a try, because nothing worked and still not works).

You mean doing it like this…?

$w.onReady(function () { 
    $w('#dataset1').onReady(()=>{
        $w('#dataset1').refresh()
    })
})

function delete_Data (parameter) {console.log("DELETE-DATA")
    wixData.truncate("Tarif")
    .then( () => {
        console.log("All items removed");
    })
    .catch( (err) => {
 let errorMsg = err;
    });
}

Sorry, nothing hapens. Do not understand why nothing hapens.
Its the same code like in the API…

wixData.truncate("myCollection")
  .then( () => {
    console.log("All items removed");
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

But i think, now i see the light in the dark. Could the infunctionality be caused, because i am not logged-in and i have no permissions because of this? :sweat_smile::sweat_smile::sweat_smile:

Ok, that was it xDDDDD. Yisrael gave me a hiden answer, i did not recognice!

Small correction… as Ahmad shows in the above snippet, the dataset’s onReady() will only run once, when the dataset is ready. So it won’t help russian-dima, it won’t do anything. However you can refresh the dataset like this

    .then( () => {
        console.log("All items removed");
        $w('#dataset1').refresh()  
    })

Or, you can just clear the repeater like this:

$w('#dataset1').data = []

Sorry, I’m working on two things while at the same time I’m on a video conference.

Ahmad is a rock star.

Exactly Yisrael, that is absolut the case! Do not work in the onReady (page-loading) and your suggestion is exactly what i did, and i spend a lot of time to recognice it.

This is by the way the whole code…

import wixData from 'wix-data';
import wixUsers from 'wix-users';

var myValue

$w.onReady(function () { })

export function button1_click(event) {myFunktion()}
export function button2_click(event) {myFunktion2()}
export function BTNreset_click(event) {delete_Data()}
export function IMGclose_click(event) {$w('#BOXcode').hide()}
export function BTNshowcode_click(event) {$w('#BOXcode').show()}

export function switch1_change(event) {
 if ($w('#switch1').checked==false) {console.log("parseFloat = OFF")}
 else {console.log("parseFloat = ON")}
}

export function switch2_change(event) {
 if ($w('#switch2').checked==false) {console.log("Database = OFF"), $w('#table1').hide(), $w('#IMGdb').hide()}
 else {console.log("Database = ON"), $w('#table1').show(), $w('#IMGdb').show()}
}

function myFunktion (parameter) {
 let user = wixUsers.currentUser;
    console.log(user)
 let userId = user.id;
    console.log(userId)
 
 let inValue1 = $w('#input1').value
 let inValue2 = parseFloat($w('#input1').value);

 if ($w('#switch1').checked==false) {myValue=inValue1, console.log("parseFloat = OFF"), $w('#TXTparsefloat').text="inactive"}
 else {myValue=inValue1, console.log("parseFloat = ON"), $w('#TXTparsefloat').text="active"}
    console.log(myValue)

 let toSave = {"prix_mois": myValue};

    wixData.save("Tarif", toSave)
    .then( (results) => {
 let item = results; //see item below
        $w('#dataset1').refresh()
    } )
    show_INFO(1, myValue)
}

function myFunktion2 (parameter) {
 let user = wixUsers.currentUser;
    console.log(user)
 let userId = user.id;
    console.log(userId)
 let inValue1 = $w('#input2').value
 let inValue2 = parseFloat($w('#input2').value);
    console.log(inValue1)
    console.log(inValue2)

 if ($w('#switch1').checked==false) {myValue=inValue1, console.log("parseFloat = OFF", $w('#TXTparsefloat').text="inactive"), console.log(myValue)}
 else {myValue=(inValue2), console.log("parseFloat = ON"), console.log(myValue), $w('#TXTparsefloat').text="active"}
 
 let toSave = {"prix_mois": myValue};

    wixData.save("Tarif", toSave)
    .then( (results) => {
 let item = results; //see item below
        $w('#dataset1').refresh()
    } )
    show_INFO(2, myValue)
}

function show_INFO (parameter, myValue) {
    $w('#BOXinfo').show()

 if (parameter==1) {$w('#TXTparsefloat').hide()}

 else {$w('#TXTparsefloat').show()}
    $w('#TXTresult').text=myValue.toString()
    setTimeout(()=>{$w('#BOXinfo').hide(), $w('#TXTparsefloat').hide()},2500)
}

function delete_Data (parameter) {console.log("DELETE-DATA")
    wixData.truncate("Tarif", {suppressAuth:true})
    .then( () => {
        $w('#dataset1').refresh()
        console.log("All items removed");
    })
    .catch( (err) => {
 let errorMsg = err;
    });
}

The only thing was, that i wasn’t logged in (NO PERMISSION to delete data, although the databse has all permissions) :roll_eyes:

So now, where my aim is clear, would you still recommend not to use datasets and data-collection the way i did? If so, why i should take care when mixing this two commands/elements?

russian-dima …

The truncate() function returns a Promise that resolves after all items have been removed from the specified collection.

It can take some time until the promise is fulfilled, in the meantime, you should stay on the same page, or simply run the function in the Backend and return the status of the operation to the Frontend if you need to know the result.

I don’t quite understand what’s not working, does the truncate() function return an error? You’re not logging the error or getting it, try logging it and see what error message you get.

.catch( (err) => {
 console.log(err);
});

And what do you exactly want to do after clearing the collection?

Thanks Ahmad,
but already claryfied the answer. Yes i know about the delay, that it takes a few seconds, because of the communication, but although i know that, i often struggle with things like (async/await - onReady or .then()) the problems come again and again :grin:.
But this time everything was fine, the only thing i did not do is, to recognice that without a log-in nothing could be deleted in the data-collection.

It’s working now (when user logged-in).

I don’t have worked with backend yet, this will be my next step/content, which i want to learn/study.

Thanks for your advices.

P.S.: Yes the error-log-command is also in my code, even if i am not so good in using it.

Here are a couple of articles that help understanding asynchronous programming:

By the way russian-dima, why do you want to clear the collection each time. Do you really need to clear it? How about using filters?

:grin: I already studied this two posts twice or tripple-times :grin:
But theory and practise is not alway the same.

Sometimes everything works like a charm, but sometimes (damn) …:rofl:
Need more practise!

Because it is maden just as an example for practise.
Don’t know how to describe, but i create a little type of my own Wix-Lexikon :grin:
where i want to be able to surf much faster and learn faster the Wix-Corvid-technics.

This is what i mean (learning by doing & doing by learning) :grin:
https://russian-dima.wixsite.com/meinewebsite/blank