Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.47k stars 587 forks source link

Property 'name' does not exist on type 'Model' #1826

Open rizdaputra opened 3 weeks ago

rizdaputra commented 3 weeks ago

i am trying to do batch create but when i prepareCreate i receive this error when following the examples given, can anyone help?

let preparedCreation = []
      defaultServices.forEach(service => {
        preparedCreation.push(database.collections.get('service').prepareCreate((newService) => {
          newService.name = service.name;
          newService.baseUrl = service.baseUrl;
          newService.port = service.port;
          newService.type = service.type;
          newService.checked = service.isChecked;
        }));
      })

      await database.batch(...preparedCreation);

this is roughly my code, the newService is detected as Model and it is said that Model dont ave name baseUrl etc.

this is my model definition

export default class Service extends Model {
  static table = 'service'

  @field('name') name! : string
  @field('type') type! : string
  @field('base_url') baseUrl! : string
  @field('port') port! : string
  @field('checked') checked! : boolean

}
KrisLau commented 3 weeks ago

why is there an exclamation mark after the column names?

rizdaputra commented 3 weeks ago

if i dont put exclamation mark this is the error i get

Property 'name' has no initializer and is not definitely assigned in the constructor.

i am developing in react native typescript

KrisLau commented 3 weeks ago

@rizdaputra Ahh ok I'm not really familiar with typescript, sorry!I actually just realized 2 things that might be your issue:

GitMurf commented 2 days ago

I do not think this is directly related to my issue, but to be honest I do not see many people responding to any GitHub Issues recently so hopefully @KrisLau or @rizdaputra can provide some feedback / advice on my issue with create / prepareCreate 🙏

I am able to get my DB creates to write to my sqlite database fine but the non core properties (i.e., not id, _status, _changes) are always just writing as empty / null values. In my example I simply just have one new field called title to keep is simple.

[!NOTE]

  1. <Testing2> is a TypeScript thing (Testing2 is name of my model for 'testing' table) so if you are not familiar with TypeScript, you can ignore it as it is just for type safety / annotations within IDE.
  2. I know above it was mentioned about awaiting db.get but actually it is not async. But I tried it that way as well just for completeness sake.
  3. create() is async which is why you see an await in my first example below, but prepareCreate is not, so no await (second example below). You can see this in the Watermelon api types file.

image

I have tried with a full write transaction / create:

await newDb.write(async () => {
  const newRow = await newDb.get<Testing2>('testing').create((theRow) => {
    theRow.title = 'title 1';
  });
  console.log('newRow:', newRow);
});

As well as just trying to test using prepareCreate before writing it:

const newRow = newDb.get<Testing2>('testing').prepareCreate((theRow) => {
  theRow.title = 'title 1';
});
console.log('newRow:', newRow);

In either case, I get the following where I see my title set properly on the record object (title set to title 1) but as you can see, the _raw record property just has title set to '' empty string, instead of the value title 1.

image

My assumption is that the actual writing to sqlite uses the _raw record which is why I get results like this, where the title is always just set as an empty string:

image

Do you know what I could be doing wrong? It seems like at some point in the process, WatermelonDB is supposed to "merge" any properties we set like this.row = "title 1" into the _raw property of the record, but it seems like that is not happening?

I am just following the example from the docs here: https://watermelondb.dev/docs/CRUD#create-a-new-record

image

Thanks in advance for any help, thoughts or feedback you can provide!