Lightning Fryer
Lightning Fryer

Reputation: 21

When using pocketbase with svelte, pocketbase returns a 'fetch failed' error unless an instance of pocketbase is running using ./pocketbase serve'

So I have a simple svelte app in which I want to perform CRUD operations using pocketbase. I have setup a collection "user" in pocketbase which has a table of fields name and age. I tried to perform a create operation using svelte actions which accepts a form data (containing the name and age) and creates a new user in the collection.

import PocketBase from 'pocketbase'
import { SECRET_EMAIL, SECRET_PASS } from '$env/static/private'

export const actions = {
    create: async({request}) => {
        const pb = new PocketBase("http://127.0.0.1:8090");
        await pb.admins.authWithPassword(SECRET_EMAIL, SECRET_PASS);

        const form = await request.formData();
        const name = form.get("name");
        const age = form.get("age");

        const newRecord = {
            name,
            age,
        }

        const record = await pb.collection("users").create(newRecord);
    }
}

But whenever I ran the dev server and tried to submit the form it would return the following error:

ClientResponseError 0: Something went wrong while processing your request.
    at file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/pocketbase/dist/pocketbase.es.mjs:1:32313
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AdminService.authWithPassword (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/pocketbase/dist/pocketbase.es.mjs:1:10785)
    at async create (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:11:9)
    at async Module.handle_action_request (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:131:18)
    at async Module.render_page (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:70:20)
    at async resolve (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:457:18)
    at async Module.respond (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:330:20)
    at async file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22 {      
  url: '',
  status: 0,
  response: {},
  isAbort: false,
  originalError: TypeError: fetch failed
      at node:internal/deps/undici/undici:12442:11
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async AdminService.authWithPassword (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/pocketbase/dist/pocketbase.es.mjs:1:10785)
      at async create (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:11:9)
      at async Module.handle_action_request (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:131:18)
      at async Module.render_page (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:70:20)
      at async resolve (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:457:18)
      at async Module.respond (eval at instantiateModule (file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55068:28), <anonymous>:330:20)
      at async file:///C:/Users/User/Desktop/VSC%20temp/svelte/pb/app/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22 {    
    cause: Error: connect ECONNREFUSED 127.0.0.1:8090
        at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16)
        at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
      errno: -4078,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 8090
    }
  }
}

But the issue is resolved and I am able to perform the create operation successfully when I start the pocketbase server manually using ./pocketbase serve in a new terminal.

1st terminal runs the dev server and second terminal runs the pocketbase server

My question is am I supposed to run an instance of pocketbase manually like this or am I doing something wrong? My assumption is that the line const pb = new Pocketbase("http://127.0.0.1:8090"); starts the pocketbase server without my doing so manually.

And if I am supposed to do this then could you please also tell me what other steps I should take when building the app to deploy it? Thanks a lot!

Upvotes: 1

Views: 225

Answers (1)

astropos
astropos

Reputation: 33

This is expected. The line

const pb = new Pocketbase("http://127.0.0.1:8090");

is simply connecting an instance of a Pocketbase to a pocketbase server hosted elsewhere, in this case at 127.0.0.1:8090

So you indeed have to run your own Pocketbase instance somewhere. For instance http://127.0.0.1:8090 means that you run the instance on the current machine (port 8090).

Upvotes: 1

Related Questions