Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rizdaputra opened this issue Aug 23, 2024 · 4 comments
Open

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

rizdaputra opened this issue Aug 23, 2024 · 4 comments

Comments

@rizdaputra
Copy link

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
Copy link
Contributor

KrisLau commented Aug 25, 2024

why is there an exclamation mark after the column names?

@rizdaputra
Copy link
Author

rizdaputra commented Aug 25, 2024

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
Copy link
Contributor

KrisLau commented Aug 26, 2024

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

  • The database.batch needs to be inside a database.write. Example from my project:
     await database.write(async () => {
       await database.batch(user);
     });
     ```
  • The table needs to be fetched before calling prepareCreate on it. Example for the user I used in my above code:
      const user = (await database.get('user')).prepareCreate(x => {
         // data assignments
      });
    Notice the brackets around the (await database.get('user'))

@GitMurf
Copy link

GitMurf commented Sep 16, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants