Unable to get full list of users using NextJs with pocketbase? #5010
-
Standalone index.js usage of pocketbase is workingI have a pocketbase instance running with few users created in dashboard. And I have created a standalone index.js in an npm project like so(I have installed pocketbase sdk as well) const PocketBase = require('pocketbase/cjs');
const pb = new PocketBase('http://127.0.0.1:8090');
const main = async () => {
const authData = await pb.admins.authWithPassword('admin@gmail.com', '<pass>');
console.log("🚀 ~ main ~ authData:", authData)
console.log(await pb.collection('users').getFullList());
};
main(); And when I run Similar nextjs usage of pocketbase not workingNow I have created a new nextjs project(from their docs, it uses the new app router). And then I have created a import PocketBase from 'pocketbase';
export default async function initPocketBase() {
const pb = new PocketBase('http://127.0.0.1:8090');
const authData = await pb.admins.authWithPassword('admin@gmail.com', '<pass>');
console.log('🚀 ~ main ~ authData:', authData);
console.log(await pb.collection('users').getFullList());
} And then in one of the main page.tsx files I have something like below import initPocketBase from '@/lib/pbClient';
export default async function Home() {
const pb = await initPocketBase();
return <main className="flex min-h-screen flex-col items-center justify-between p-24">Main Page</main>;
} Now when I check the nextjs logs(in the server), I only see authData being printed, but the users full list is an empty array(its being printed as []). So at least I know that I am able to connect to pocketbase since admins authData is printing, but full list of users are not being printed. I thought this might be an issue with ListRule access in pocketbase but I dont think thats the case because in standalone index.js example(first example above) it works perfectly fine. So is this an issue with nextjs or some finicky stuff with async-await in pocketbase? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So if I change pocketbase host url from 127.0.0.1 to localhost, then it seems to work. Not sure why this is the case. This SO answer had the hint stackoverflow.com/a/76862703/6059506 |
Beta Was this translation helpful? Give feedback.
If you were able to get empty full list array before the hostname change then your issue is not related to that (and I recommend for local development to stick with
127.0.0.1
because of pocketbase/js-sdk#21).What you are experiencing is probably the Next.js default cache and I'm getting really tired by this. For opting out please check "Opting out of Data Caching" but in any case this is not really a PocketBase issue, especially since you've confirmed that it works without Next.js. The JS SDK doesn't do any "finicky stuff with async-await" and uses whatever global
fetch
implementation is available.