Querying Issues #5011
-
SVELTEKIT + POCKETBASE Issue To clarify -> Creates the match, users are assigned to the match, but not vice versa. (only one user is assigned). CODE import type { RequestHandler } from '@sveltejs/kit';
export const POST: RequestHandler = async ({ locals, request }) => {
try {
const { user_id } = await request.json();
console.log('Received user_id:', user_id);
// Split the players into two teams
const attackers = user_id.slice(0, 1);
const defenders = user_id.slice(1, 2);
console.log('Attackers:', attackers);
console.log('Defenders:', defenders);
const match = await locals.pocketbase.collection('match').create({ attackers, defenders });
console.log('Match created:', match);
const matchAttackers = match.attackers;
const matchDefenders = match.defenders;
console.log('Match Attackers:', matchAttackers);
console.log('Match Defenders:', matchDefenders);
const defenderUsers = await locals.pocketbase.collection('users').getFullList({ filter: `id = "${matchDefenders}"` });
const attackerUsers = await locals.pocketbase.collection('users').getFullList({ filter: `id = "${matchAttackers}"` });
console.log('Defender Users:', defenderUsers);
console.log('Attacker Users:', attackerUsers);
if (!defenderUsers || !attackerUsers) {
return new Response(JSON.stringify({
status: 404,
body: { message: 'One or both users not found' }
}), {
status: 404,
headers: { 'Content-Type': 'application/json' }
});
}
return new Response(JSON.stringify({
status: 200, // OK
body: { message: 'Match successfully created' }
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' }
});
} catch (error: any) {
console.error('Error:', error);
return new Response(JSON.stringify({
status: 500,
body: { message: 'An error occurred', error: error.message }
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}; Response: 7:09:16 AM [vite] page reload src/routes/api/matches/create/+server.ts
[]
[]
Received user_id: [ 'j672dfqy74nexgk', 'mb1e5b9yq5hw4j4' ]
Attackers: [ 'j672dfqy74nexgk' ]
Defenders: [ 'mb1e5b9yq5hw4j4' ]
Match created: {
attackers: [ 'j672dfqy74nexgk' ],
collectionId: 'h4o0pa6kbvtswu7',
collectionName: 'match',
completed: false,
created: '2024-05-31 06:09:31.734Z',
defenders: [ 'mb1e5b9yq5hw4j4' ],
id: 'cztic5xpcvezmz8',
updated: '2024-05-31 06:09:31.734Z'
}
Match Attackers: [ 'j672dfqy74nexgk' ]
Match Defenders: [ 'mb1e5b9yq5hw4j4' ]
Defender Users: []
Attacker Users: [
{
avatar: '',
avatarUrl: 'https://lh3.googleusercontent.com/a/ACg8ocIT8abcgmevgV9k588iO-0xZrqo-WoqS9Bi1oQHs6j-dl5H5fYf=s96-c',
collectionId: '_pb_users_auth_',
collectionName: 'users',
created: '2024-05-27 08:32:55.308Z',
email: '',
emailVisibility: false,
id: 'j672dfqy74nexgk',
matches: [],
name: 'Adrian',
updated: '2024-05-29 10:40:36.241Z',
username: 'Adrian',
verified: true
}
] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm not sure that I understand the described issue but if If you want a filter that match records from a list of ids you can do something like: // aka. id="id1" || id="id2" || ...
filter: matchDefenders.map((id) => `id="${id}"`).join("||") |
Beta Was this translation helpful? Give feedback.
Additionally note that you'll get empty
getFullList
result if your client request doesn't satisfy the List API rule of theusers
collection.