Hi Chris
You have a few problem areas one is that you have more than one record with the same Owner Id.
So your dataset filter will filter to a set of 7 records. Not the one you are thinking of
Let me walk you through your code and make one or two suggestions:
$w.onReady( function () {
// let user = wixUsers.currentUser; <<< This is the correct way to get the logged in user
//let userId = user.id ; <<<<< This should be === “5c4794ca-8ac7-4193-9e61-6bf81eb53307”
let item1 = $w(" #dataset2 ")
let pokojenumery = item1.pokoj <<<<< This is an error so pokojenumery === null
The code from this point will not run because of the above error. $w(‘#dataset2’) is like a box of information which is described by the wix-dataset API .
If you want to get the pokoj value you need to first get an item from the dataset. You do this like so:
$w("#dataset2").getCurrentItem().pokoj;
or
item.getCurrentItem().pokoj;
Now you can check how many items are in the filtered dataset you do this
let numberOfItems = $w("#dataset2").getTotalCount();
console.log("Filtered Dataset contains: "+numberOfItems);
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
wixData.query(“pokoje”) |
vvvvv pokojenumery is a reference value which needs to be tested against _id |
.eq(“title”, pokojenumery) |
// .eq(“_owner”, userId) |
.find() ^
.then( (resultsc) => { ^
console.log(resultsc) ^
$w(“#repeater3”).data = resultsc.items |
|
This is a handler function and should be declared before the wixData.query function.
$w(#repeater3
).onItemReady(($w, itemData) => {
$w(`#text36`).text = resultsc.title; <<< resultsc is an array of items. This should be itemData.title
$w('#text40').text = resultsc.standard; << resultsc should be itemData.standard
})
});
});
Your code needs to be more like the code below BUT the code will only work if you can make sure your zakwaterowanie data set only has one record per logged in user.
$w.onReady(() => {
// Define repeater onItemReady handler
$w(`#repeater3`).onItemReady(($item, itemData) {
$item(`#text36`).text = itemData.title;
$item('#text40').text = itemData.standard;
});
let user = wixUsers.currentUser;
let userId = user.id;
let pokojenumery = $w("#dataset2").getCurrentItem().pokoj;
wixData.query("pokoje")
.eq("_id", pokojenumery)
.find()
.then( (resultsc) => {
console.log(resultsc) ;
$w("#repeater3").data = resultsc.items;
}
});
Now you also said that you want to show all rooms that are free. So you need to find all records in the “pokoje” dataset that DO NOT have _ids that are registered in the “zakwaterowanie”.
To do this you need to get all records in the “zakwaterowanie” dataset that have values set as Owner. Here is one way to do this
import wixData from 'wix-data';
...
function getEmptyRooms() {
// Get list of occupied rooms (we return a Promise so that the calling function can
// process the results
return getOccupiedRooms()
.then((occupiedRoomList) => {
let emptyRoomQuery = wixData.query("pokoje");
// loop through occupied rooms creating a list of queries to exclude occupied rooms
for (let i = 0 ; i < occupiedRoomList.length; i++) {
// Get a record to exclude
let occupiedRoomRecord = occupiedRoomList[i];
let exclusionQuery = wixData.query("pokoje").eq("_id", occupiedRoomRecord.pokoj);
emptyRoomQuery = emptyRoomQuery.not(exclusionQuery);
}
// We have built our Empty Room Query - now execute it
return emptyRoomQuery.find();
})
.then((results) => {
return Promise.resolve(results.items);
});
}
function getOccupiedRooms() {
return wixData.query("zakwaterowanie")
.isNotEmpty("_owner")
.find()
.then((result) => {
// Return a promise with the array of occupied rooms
return Promise.resolve(result.items);
})
}
To use the emptyRooms function you use the Promise .then() function like this:
getEmptyRooms()
.then((emptyRoomsList) => {
// Add code here to process the emptyRoomsList array
});
Hope this makes sense. The document references that you should use to try to understand the code are:
Steve