TypeError: Cannot read property '_id' of undefined

Hi there!
The dataset is not able to filter and the above error is shown. I think it’s because the ._id is being called even before it has a value, but I can’t figure out the mistake. Below is the code. Any help is appreciated!

import wixData from ‘wix-data’;

let path = wixLocation.path;
let name = path[0].toString();

$w.onReady(function () {

$w("#readMe").onReady(() => { 
    wixData.query("Members/PrivateMembersData") 
        .eq("slug", name) 
        .find() 
        .then((results) => { 
            let finalId = results.items[0]._id; 
            $w("#readMe").setFilter(wixData.filter() 
                .eq("userId", finalId) 
            ) 
                console.log(finalId); 
        }).catch(error => { 
            console.log(error); 
        }); 
});

The query probably gets rejected due to security&privacy.

You have to run the query from the backend and to have .find({suppressAuth: true})

And to import wixLocation

Thanks. @jonatandor35 The issue here is, wixLocation can only be used in the FrontEnd. What I’m trying to do is get the id of the member from a member page, when someone views it (say, profile). Any idea on what’s the best way to do it…?

@wolftechnologiesin use wixLocation in the front end, import the backend module (which you should put in a jsw file) to the front end and call it from there.

@jonatandor35
Have this code in getOwnerId.jsw

let options = {
“suppressAuth”: true
};

let path = wixLocation.path;
let name = path[0].toString();

export function getOwnerId(userId){

return wixData.query(“Members/PrivateMembersData”)
.eq(“slug”, name)
.find(options)
.then( (results) => {
let finalId = results.items[0]._id;
return finalId;
});
}

But, since the name is getting stuff from the URL, I’m getting an error: wixLocation is not defined. If I import wixLocation to the jsw file, this happens:

Frontend code:

import wixLocation from ‘wix-location’;
.
.
.
$w.onReady(function () {
$w(“#readMe”).onReady(() => {
console.log(“ready”);
getOwnerId()
.then(ownerId => {
$w(“#readMe”).setFilter(wixData.filter()
.eq(“userId”, ownerId)
)
console.log(ownerId);
}).catch(error => {
console.log(error);
});
});
});

@wolftechnologiesin you can’t have let path = wixLocation.path in the backed. You have to pass it as parameter from the front end to the backend.

@jonatandor35 Thanks a lot. Still learning :grinning::v:t2: