Please HELP with query of two collecions combined

Hi i have two datasets. Pokoje which menas “Rooms” and other zakwaterowanie means “accomodation” . “rooms” have noumbers and information about rooms. Accomodation have information of user who take the room and write who will live in a room. Ispend lots of tiime trying to solve it but its above me. Please help me. dataset accomdation is sorted by log in user. I tried to make filter in database pokoje but it doeasnt work becasue there is refernce field room noumber. I want at one page see my rooms and not alll rooms but only free rooms. So one which are not taken by me. Probably solution is easy but i cant .

https://misiewicz3.wixsite.com/mysite/kopia-pokoje
user info@ceglarnia.com pass: info

i forgot write my lets say code :slight_smile: i was thinking of query that compare two datasets and give results to repeater

$w.onReady( function () {

// let user = wixUsers.currentUser;
//let userId = user.id;
let item1 = $w(“#dataset2”)
let pokojenumery = item1.pokoj
wixData.query(“pokoje”)
.eq(“title”, pokojenumery)
// .eq(“_owner”, userId)
.find()
.then( (resultsc) => {
console.log(resultsc)
$w(“#repeater3”).data = resultsc.items
$w(#repeater3).onItemReady(($w, itemData) => {
$w(#text36).text = resultsc.title;
$w(‘#text40’).text = resultsc.standard;
})
});
});

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

@stevendc WOW so i understand that my code would work after small changes if one user have only one room i zakwaterowanie. I saw Your code and is great. I would never find it out without Your help. As i understand after Your code i need to start function and put array to repeator like this?

getEmptyRooms()
.then((emptyRoomsList) => {

   $w("#repeater3").forEachItem( ($item, itemData, index) => {   
    $w(`#text36`).text = itemData.title; 
    $w(`#text40`).text = itemData.standard; 
    $w(`#text46`).text = itemData.dostawkimax; 

})
})

@ceglarnia Nearly! You are missing the crucial step of assigning the emptyRoomsList to the repeater data property.

getEmptyRooms() .then((emptyRoomsList) => {
    $w("#repeater3").data = emptyRoomsList; // Add this
    $w("#repeater3").forEachItem( ($item, itemData, index) => {
        $w(`#text36`).text = itemData.title;         
        $w(`#text40`).text = itemData.standard;         
        $w(`#text46`).text = itemData.dostawkimax;
    });
});

Cheers
Steve

@stevendc sorry its me again :slight_smile: because it shows i think good quantity of repeators but all with same valueas AS if there was something wrong with putting it to repeator. i disconnected it with dataset 1. Its good? As i understand now i put values manually by code so it thosnt make a sense to connect in editor

getEmptyRooms() .then((emptyRoomsList) => {
$w(“#repeater3”).data = emptyRoomsList; // Add this
$w(“#repeater3”).forEachItem( ($item, itemData, index) => {
$w(#text36).text = itemData.title;
$w(#text40).text = itemData.standard;
$w(#text46).text = itemData.dostawkimax;
$w(#text48).text = itemData.lokalizacja
$w(#text38).text = itemData.uwagi;
$w(#gallery3).gallery = itemData.zdjeciepo

});
});

You need to understand the scoping variable :-).

A repeater is a holder for elements that have identical element names that are repeated. In order to be able to do this we need to tell the repeater element, for example ‘#text36’, which container to display in. One of the arguments to forEachItem you will see is $item. This is used to determine which container to populate. $w is the main page that the repeater is part of so you should try to distinguish which scope is being used.

The fix is to replace $w in the forEachItem call with $item for example:

$item(‘#text36’).text = itemData.title;
$item(‘#text40’).text = itemData.standard;

And so on.

Cheers
Steve

It workes great. Thank You Sooo Much. I add code for adding rooms to My rooms and is great.
But i have few warnings and error in code which put emty rooms to repeator. I dont know why few field when i put to repeator have those warnings. With g2 field and dostawki max is strange because value shows right.
With gallery its ok because i cant find anywhere code for gallery. I mean how to put gallery pictures to repeator ??
Wix code SDK error: The text parameter that is passed to the text method cannot be set to the value 0. It must be of type string.moje pokojeLine 53Wix code SDK Warning:
The items parameter of “gallery3” that is passed to the items method cannot be set to null or undefined

@ceglarnia Hi Chris: Can I ask you to do me a favour? :slight_smile:
When you encounter a new problem can you create a new post?
It is possible that this post helps give clues to the new problem OR there may be no relationship at all. One of the values of a great forum is being able to find ideas in problems with solutions. This is hard to do if the title of the post doesn’t make sense based on the discovered problem, or multiple problems are answered in one post (which was the answer to the original post and what other problems are being addressed?). Lastly tagging an answer as Top Comment if it resolves the issue helps other forum members find answers and build trust in the members giving those answers.

When you create a new post please make sure you reference this one (in case it helped highlight the new problem) but try to focus the new post on the new problem :wink:

I’ll do my best to help you (or you may find another able forum assistant!).

Cheers
Steve

You wright. i didt thought of it. I will make new post. I alweys tag as top Comment.

1 Like

@stevendc i just noticed that i put Top comment every time but it didnt save . I refreash website and there is no top comment. DO i have to save it somewhere?

maybe someone needs to approve those therefore they were not saved

@ceglarnia No problem Chris. You have been pretty good with the Top Comment button :wink:

@stevendc Hi I have question. Is it possible to get contact to You. Or are You on Wix Arena ? I have few small problems with code and look for someone who can help me with it. In case my email. info(at)ceglarnia.com