In Velo, I am trying to compare the owner ID of already created posts with the ID of current logged-in user.
For this, I want to recall a back-end function that produces the current user ID ("_owner") in use it in a client-side query function. I have been just exhausted of retrieving the "_owner", which I am going to use in the query function, correctly. I know from console.log(memberId) I get the ID inside the query function, but I'm not able to use it as value for the the query function. Any help please?
In the back-end side I have this code:
import { currentMember } from 'wix-members-backend';
export function myGetCurrentMemberFunction() {
return currentMember.getMember()
.then((member) => {
const memberId = member._id;
return memberId;
})
}
In the client-side I have this code:
import { myGetCurrentMemberFunction } from 'backend/backendfunctions.jsw'
function matchPostCurrentUser(event) {
// Matching current user ID with that of the entered post ID
wixData.query("Collection1")
.eq("_owner", myGetCurrentMemberFunction()
.then(memberId => {
console.log(memberId)
memberId
})
)
.find()
.then((results) => {
let result = results.items
console.log(result)
if (results.items.length === 0) {
console.log("You choosed other users post's ID" + result )
}
})
}
Look like you've made a bit of a mess of your client-side code. The easiest, and probably most readable way to do this is using async
/await
instead of .then
with the promises.
That would look something like this:
async function matchPostCurrentUser(event) {
const currentMemberId = await myGetCurrentMemberFunction();
const results = await wixData.query("Collection1")
.eq("_owner", currentMemberId)
.find();
console.log(results.items);
// etc...
}
If you really want to use .then
, it would look something like this:
function matchPostCurrentUser(event) {
myGetCurrentMemberFunction()
.then(memberId => {
return wixData.query("Collection1")
.eq("_owner", memberId)
.find();
})
.then((results) => {
let result = results.items;
console.log(result);
if (results.items.length === 0) {
console.log("You choosed other users post's ID" + result );
}
});
}