Common mistakes
These are the mistakes people most often make when they build their first app on phibac. Each says what goes wrong, how to fix it and which guide covers it. When your app gets an error code, Errors and how to fix them explains each one.
Signing in
Calling connect() outside a click
The mistake: calling connect() when the page loads, from a timer, or after an await in a click handler, and expecting it to sign the user in.
What goes wrong: when no sign-in is stored in the browser, connect() must open a popup on the server, and browsers allow a popup only as a direct result of a click or key press. Anywhere else it fails with popup_blocked.
The fix: call connect() first in a click handler, before any await. On page load you can still call it to pick up a sign-in that is already stored: it then needs no popup, and when there is none it fails with popup_blocked, so show a Sign in button. See Sign users in.
The same rule holds for requestAccess(), which opens the consent popup: call it from a click.
Developing on a localhost port you did not register
The mistake: registering http://localhost:5173 and then serving the page from another port, or from 127.0.0.1.
What goes wrong: a page on an http://localhost origin (the scheme, host and port a page is served from) that is not registered can still sign in, but the server asks the user to approve your app at every sign-in. 127.0.0.1 is a different origin and gets no such exception: sign-in is refused there.
The fix: register the exact origin you develop on, port included. See Develop on your own machine.
Forgetting to register the live page's origin
The mistake: putting the app online without registering the origin it is served from, or registering it without the www the page uses, or with another port.
What goes wrong: the sign-in popup says the app asked to sign in from an address it has not registered, and requests from that page are refused with forbidden.
The fix: register the exact https:// origin of the live page. See Put your app on the internet.
Defining and querying data
Forgetting indexed on the fields you query
The mistake: filtering or sorting on a field that is not marked indexed in pb.config.ts.
What goes wrong: online, a query's get() fails with invalid_request, saying the field "cannot be used to filter or order". The mistake is easy to miss, because subscribe() and an offline get() run on the browser's copy of the data and do not check indexes.
The fix: add indexed: true to every field you filter or sort on, then run npx phibac deploy and npx phibac generate. Adding indexed is not a breaking change. createdAt and updatedAt can always be used. See Query, sort and live updates.
Using a sandbox collection for data meant to be shared
The mistake: keeping data in a sandbox. collection, which needs no schema, and expecting other apps to read it later, or to filter and sort it.
What goes wrong: a sandbox collection is private to the app that writes it. Another app that uses the same name gets its own separate collection, and the user can never give another app permission to read yours. It has no indexed fields, so queries on it can filter and sort only on createdAt and updatedAt.
The fix: use a sandbox collection only for data only your app needs, such as settings or drafts. For data that other apps may read, or that you want to query, publish a schema under a real name such as com.example.notes.note. See Define your data and Collections and records.
Expecting joins or counts
The mistake: expecting a query to fetch the records a record points to (a join), or to return a count, a sum or an average.
What goes wrong: there are none. A recordRef field holds the other record's ID, and reading a record never returns the record it points to. Counting the results of a query without limit(n) gives a wrong number once there are more than 500 matching records, because a query stops there by default.
The fix: look linked records up yourself: take the rkey from the end of the ID and call get(rkey) on the other collection, or read the other collection once with list() and find records by rkey. Both read the browser's copy of the data, which may not have every record yet right after sign-in, so handle a record that is not found. For a count or a total, read the records and compute it in your code, with a limit(n) larger than the collection can ever get. See Query, sort and live updates.
Writing before the collection is published
The mistake: writing to a new collection before running npx phibac deploy.
What goes wrong: create() returns at once, because the change is queued, but the server refuses it with not_found, saying the collection "has no published schema". The SDK drops the change and reports it through onConflict.
The fix: run npx phibac deploy first, then npx phibac generate. See Define your data.
Changing a field in place
The mistake: removing, renaming or retyping a field, or making it required, without a new major version.
What goes wrong: npx phibac deploy shows the collection as breaking (the server's code is schema_incompatible) and publishes nothing.
The fix: publish the change as the next major version by raising the collection's version by one, or make it additive instead, for example with a new optional field. See Change your schema safely.
Writing data
Storing a counter in a record
The mistake: keeping a running count or total in a record and changing it from several devices, such as a likes field that each device increases by one.
What goes wrong: when two devices change the same field, the server applies the changes in the order it receives them and the last one wins. Two increments made offline on two devices overwrite each other, so the count is lost silently: no error is raised.
The fix: store one record per event and add them up in your code. If you keep a stored total, recompute it from the records when it must be right. See Query, sort and live updates and Offline writes and sync.
Writing another app's data
The mistake: creating, changing or deleting records in a collection another app owns, after the user allowed your app to read it.
What goes wrong: access to another app's collection is read-only, with or without consent. Because writes are queued, create(), update() and delete() still return at once, and the server's refusal, forbidden, arrives later through onConflict, when the SDK drops the change.
The fix: read the other app's collection with backend.collection(name, { versions: [1] }), listing the major versions your code understands, and keep your own data in a collection your app publishes. See Read another app's data.
Not listening to onConflict
The mistake: treating the record that create() or update() returns as saved on the server, and never calling backend.onConflict().
What goes wrong: the record is saved on the device and marked pending until the server confirms it. When the server refuses it for good, for example with schema_violation, the SDK drops the change and puts the record back as the server has it. Without an onConflict function, the user's change disappears with no message.
The fix: register an onConflict function right after connect(), and show the user what was not saved; its localData holds the data your app had. Show pending records as not yet saved. See Read and write records and Offline writes and sync.
Signing out with changes still queued
The mistake: calling signOut() without checking for queued changes.
What goes wrong: signOut() deletes the user's copy of the data and their queued changes for your app from the browser. Changes that have not reached the server are lost.
The fix: check backend.status().pending first. If it is above zero, warn the user or wait until it reaches zero. See Sign users in.