Suppose you have a data collection entitled “Members”, which has various pieces of information.
I want to be able to retrieve the _id of this member, give that we know the users name and place it in a textbox on the current page.
So far I have
wixData.query(“Members”)
.eq(“name”, $w(“#name”).text)
.find()
.then( (results) => {
console.log(results.items);
$w(“#memberID”).text = results;
} );
}
This retrieves all the data for that members, but I only want the _id.
1 Like
Hi,
That’s right, all of the data is returned. Plus, the results are an array. You’re only expecting one record as a result (assuming that user names are unique), but many queries return multiple records.
You need to first ensure that you’re just retrieving one record. The items returned are in the results object, and you want to index just the first one:
let item = results.items[0]; // get first item in array of results
Then, to get a specific field (_id), just do this:
let id = item._id;
To illustrate further, if you want the user’s phone number, you might do this:
let phone = item.phone;
I hope this helps. Good luck, and have fun,
Yisrael
1 Like