Issue with Query "_id" [SOLVED]

hey guys, having a weird issue with querying the ID in a collection. If i try to find an entry based off of the ID it returns 0:

let currentitem = $w(“#dataset2”).getCurrentItem()._id;
console.log(currentitem);

wixData.query(‘Projects’)
.eq(“_id”, currentitem)
.find()
.then(res => {
console.log(res.items);
});

res.items is always empty. I’ve tried just using a known ID but not even that works. Am I overlooking something?

Hi Matt -

Maybe try to rewrite the code as follows:

CODE:
--------

let currentitem = $w(“#dataset2”) .getCurrentItem();
console.log("currentitem: " + currentitem);

wixData.query('Projects') 
.eq("_id",  **currentitem._id** ) 
.find() 
.then(res => { 
    console.log(res.items); 
}); 

I did try this code and it was working. Let me know if it works for you.

The main difference in the code is where you are trying to access the “._id” property. It appears that you need to first get the current item, and then you can access/use the current item’s properties.

Best,
Nick

Hey Nick! Thanks for the help. unfortunately I’m still having issues. The array being returned is still empty, even after I modified the code:

let currentitem = $w(“#dataset2”).getCurrentItem();
console.log(currentitem);

    wixData.query('Projects') 
        .eq("_id", currentitem._id) 
        .find() 
        .then(res => { 
            console.log(res.items); 
    }); 

Hi Matt -

Slightly different question, but if you are able to get the “currentItem” (which has all of the properties of that item) is there a reason you need to try to get the same item from the Collection?

In terms of why it still isn’t retrieving the Item when you pass the “._id”, is the Security for the “Projects” collection allow “Anyone” to Read the content? Also, is the name of the collection correct?

Best,
Nick

I just tried this (to remove something from the collection) and it also is throwing up an error:

let currentitem = $w(“#dataset2”).getCurrentItem();
wixData.remove(“Projects”, currentitem._id);

So the reason I need to get into the collection is because I am trying to connect user inputs to an “edit” button on a repeater. If I try to connect them to the dataset the user inputs only connect to the first item in the repeater (no matter which repeater element’s ‘Edit’ button I click. So now i am trying to obtain the item’s ID from the repeater and then tell the user inputs to connect to that item in the collection.

I just checked the spelling and the permissions and it doesn’t look like that’s the problem either… I feel like I’m missing something super simple but I can’t seem to figure out what it is haha. Really appreciate the help Nick!

just to be clear I am signed in as a user while I’m trying to do this.

On the dataset properties (on the page), what value do you have for “Mode”?

If it is not set to “Read-only” try changing the mode to read-only and then retry your code.

That didn’t seem to work either. Let me take a step back and explain what I’m trying to do. I am making a set of cards (a repeater). The cards can be edited, deleted, and their order can be changed. Because the repeater does not work with user inputs I have to use the repeater as just a way of selecting which collection entry I would like to interact with. I was able to get the arrows (order changers) to work just fine. Whats even more frustrating is that I used the exact method we’re having problems with now. You can see the code and cards bellow:

$w(“#repeater8”).onItemReady( ($w, itemData) => {
$w(“#moveright”).onClick((event) => {
let currentnumber = $w(“#dataset2”).getCurrentItem().order;
let nextnumber = (+ currentnumber) + 1;
wixData.query(“Featured”)
.eq(“order”, nextnumber)
.find()
.then( (results) => {
let currentcardupdate = {
“_id”: $w(“#dataset2”).getCurrentItem()._id,
“title”: $w(“#dataset2”).getCurrentItem().title,
“link”: $w(“#dataset2”).getCurrentItem().link,
“image”: $w(“#dataset2”).getCurrentItem().image,
“order”: nextnumber,
};
let nextcardupdate = {
“_id”: results.items[0]._id,
“title”: results.items[0].title,
“link”: results.items[0].link,
“image”: results.items[0].image,
“order”: (+ currentnumber),
};
wixData.update(“Featured”, currentcardupdate);
wixData.update(“Featured”, nextcardupdate);
resort();
});
});
$w(“#moveleft”).onClick((event) => {
let currentnumber = $w(“#dataset2”).getCurrentItem().order;
let prevtnumber = (+ currentnumber) - 1;
wixData.query(“Featured”)
.eq(“order”, prevtnumber)
.find()
.then( (results) => {
let currentcardupdate = {
“_id”: $w(“#dataset2”).getCurrentItem()._id,
“title”: $w(“#dataset2”).getCurrentItem().title,
“link”: $w(“#dataset2”).getCurrentItem().link,
“image”: $w(“#dataset2”).getCurrentItem().image,
“order”: prevtnumber,
};
let nextcardupdate = {
“_id”: results.items[0]._id,
“title”: results.items[0].title,
“link”: results.items[0].link,
“image”: results.items[0].image,
“order”: (+ currentnumber),
};
wixData.update(“Featured”, currentcardupdate);
wixData.update(“Featured”, nextcardupdate);
resort();
});
});
//--------pace where I add the code for the Edit and Delete buttons
});
function resort () {
$w(“#dataset2”).refresh();
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
$w(“#dataset2”).setSort( wixData.sort()
.ascending(“order”)
);
}, 10);
}

However, when I added the code for the Edit and Delete button I started having problems. Now when I query a collection the return array is just 0. It’s exactly the same method as far as I can tell, but with different result. Here is the code for both:

$w(“#delete”).onClick((event) => {///////////////////////////////DELET BUTTON
let orderstring = $w(“#dataset2”).getCurrentItem().order;
let ordernumb = (+ orderstring);
//let currentitem = $w(“#dataset2”).getCurrentItem();
//wixData.remove(“Projects”, currentitem._id);
$w(“#dataset2”).remove();
console.log(“deleting ordernumb: " + ordernumb);
orederRecalibration (ordernumb);
});
$w(”#Edit").onClick((event) => { ///////////////////////////EDIT BUTTON
$w(“#columnStrip20”).expand();
let currentitem = $w(“#dataset2”).getCurrentItem();
console.log(currentitem);
wixData.query(‘Projects’)
.eq(“_id”, currentitem._id)
.find()
.then(res => {
console.log(res.items);
$w(“#input2”).value = res.items[0].title;
$w(“#dropdown2”).value = res.items[0].link;
$w(“#uploadButton2”).value = res.items[0].image;
let editorder = res.items[0].order;
});

Where the orederRecalibration function is as follows:

function orederRecalibration (oldnumb) {
//let newnumb = (+ oldnumb) + 1;
//console.log(“moving ordernumb : " + newnumb + “to ordernumb: " + oldnumb);
//let currentnumber = $w(”#dataset2”).getCurrentItem().order;
let nextnumber = (+ oldnumb) + 1;
wixData.query(“Featured”)
.eq(“order”, nextnumber)
.find()
.then( (results) => {
if (results.length > 0) {
let update = {
“_id”: results.item[0]._id,
“title”: results.item[0].title,
“link”: results.item[0].link,
“image”: results.item[0].image,
“order”: oldnumb,
};
wixData.update(“Featured”, update);
orederRecalibration(nextnumber);
}
resort ();
});
}

I have no idea what I’m doing wrong at this point haha. Again, thanks for all the help!

Hey Matt -

So from what I can tell, you may be missing a " }); " at the end of Edit OnClick event code. When I reformatted the Delete and Edit code, this is what it looks like:

$w(“#delete”).onClick ( (event) => { ///////////////////////////////DELET BUTTON
let orderstring = $w(“#dataset2”).getCurrentItem().order;
let ordernumb = (+ orderstring);
//let currentitem = $w(“#dataset2”).getCurrentItem();
//wixData.remove(“Projects”, currentitem._id);
$w(“#dataset2”).remove();
console.log("deleting ordernumb: " + ordernumb);
orederRecalibration (ordernumb);
}) ;

$w(“#Edit”).onClick ( (event) => { ///////////////////////////EDIT BUTTON
$w(“#columnStrip20”).expand();
let currentitem = $w(“#dataset2”).getCurrentItem();
console.log(currentitem);
wixData.query(‘Projects’)
.eq(“_id”, currentitem._id)
.find()
.then ( res => {
console.log(res.items);
$w(“#input2”).value = res.items[0].title;
$w(“#dropdown2”).value = res.items[0].link;
$w(“#uploadButton2”).value = res.items[0].image;
let editorder = res.items[0].order;
}) ;
}); // - NEW

Maybe that will do the trick.

Hi Guys:

It seems to me that you are over looking the repeater scope in this code.

Each item in a repeater container has a scope that maps to the current item.

The onClick event handler takes two parameters event and $w. The $w passed to the event handler is scoped to the repeater so when you call $w(" #dataset2 ").getCurrentItem() inside a repeater element handler function you get the item for that repeater item instance.

So your mistake is that you are not letting the scope variable do its job.
$w(" #Edit ").onClick ( (event)

should be

// Note I have named the $w to $wScope to be clear that it is a different object than $w.
$w("#Edit").onClick((event, $wScoped) => {
    let currentitem = $wScoped('#dataset2').getCurrentItem();
}

This means that whenever the handler is called it is always called with the correct repeater container context. If you do this you will probably have a lot less code to write :wink:


Check out repeated item scope

Hope this helps.

Hey guys,
I can’t thank you both enough for all the help. This was driving me crazy. In the end the problem was just that I was using the ID from one collection to query the ID’s in the other. That’s why it never returned any results. I made a reference field and it’s all working great now! Again, thanks for taking time to help me. This is really a fantastic community!