Hey, Does anyone know why the filter code below does not work? Thanks

import wixData from 'wix-data';

$w("#dataset4").setFilter( wixData.filter()
.eq("offer_id", "1001")
);

You’ll need to provide more information for us to be able to assist…

Do you have a item in the database collection that has the id “1001”?
How are you displaying the results?
What page elements?
What other code do you have?

Thanks for your replay.

Basically what I am trying to do is to filter out a collection based on two elements so I can retrieve the right record.

So, basically I have a collection “users_offers” that is referring to the wix user collection and to offers collection.

I have a dynamic page per offer (based on “offers” collection) and would like to be able to filter out my “users_offers” collection so I can pull the create_date (claim_date) of a specific record filtered by user_id and offer_id.

Whenever I try to pull the creation date I keep getting the first row in the users_offers collection (dataset4).

import wixData from 'wix-data';


$w("#dataset4").setFilter( wixData.filter() //--> #dataset4 is the users_offers dataset collection

.eq("offer_id", "1001") //---> 1001 will be replaced with the offer id vlaue pulled from the dynamic dataset.

// "offer_id" is a field within the user_offers collection refering to the offers collection. 

);



//so it looks like that basically


let offers = $w("#dynamicDataset").getCurrentItem().title;

$w("#dataset4").setFilter( wixData.filter()
.eq("offer_id", offers)
);

let claim_time = $w("#dataset4").getCurrentItem()._createdDate;
claim_time = claim_time.getDate();

let date = new Date();
date = date.getDate();

if( date === claim_time) {
$w("#button5").hide();
}

I’ve gotta be honest and say I’m still having trouble understanding your code. I just don’t have enough context.

  • I don’t understand how the two segments of code that you posted are related.

  • Do you have a page onReady(), and if so, what code do you have in it?

  • Do you have a dataset onReady()? If not, perhaps your dataset is not ready before you’re trying to use it.

ok, So that is the full code I am using.
The behavior should be as such:

The website is a voucher website. Each voucher (offer) can be used only once a day.
I have created an “offers” collection based dynamic page with a claim offer button.
The way I am trying to implement it is as followed:

  1. When the claim offer is clicked (button4_click(event)).
    a. I create a new row to a users_offers collection (dataset4).
    b. The user is directed to a thank you page.

  2. When a user is back on the dynamic offer page, in the onReady function I check if the user already claimed the offer today or not by comparing today’s date to the claim_date (create date on the users_offers collection).
    ** Currently I am able to only pull the first row in the users_offers collection and not the row that includes the current user and offer. **

  3. If the claim_date and today’s date are the same, I hide the claim button (button4).

  4. (Did get here yet with my code) if the claim_date is today, another notification element will appear instead of the claim button (button4).

Hope it makes things more clear.
It is my first time writing so sorry if it is a bit messy.

Thanks for all your help

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

$w.onReady(function () {
$w("#dataset4").onReady(() => {

let offer_id = $w("#dynamicDataset").getCurrentItem().title;
wixData.get("Offers", offer_id)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );

$w("#dataset4").setFilter( wixData.filter()
.eq("offer_id", offer_id)
);

//--------> make button disapear if click date = today
let claim_time = $w("#dataset4").getCurrentItem()._createdDate;
claim_time = claim_time.getDate();
let date = new Date();
date = date.getDate();
if( date === claim_time) {
$w("#button4").hide();
}
})
});

// --> onclick- new row in users_offers collection
export function button4_click(event) {
let user = wixUsers.currentUser;

// On click --> get user id
let title = $w("#dynamicDataset").getCurrentItem();
let userid = user.id;

// insert parameters
let toInsert = {
"user_id" : userid,
"offer_id": title,
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
wixData.insert("Users_Offers", toInsert, options)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
}