Nozbe / WatermelonDB

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

Error in deleting Records (Writer Method [TypeError: undefined is not a function]) #1657

Open ghost opened 1 year ago

ghost commented 1 year ago

Hi, I have problems deleting (or even manipulating) records. Hope you guys can help. I followed the instructions from the online document, however, no luck.

Here is one of my schema:

// model/ingredient.js
import { Model } from '@nozbe/watermelondb' 
import { field, date, readonly, text} from '@nozbe/watermelondb/decorators'
import { Q } from '@nozbe/watermelondb';
import { writer } from '@nozbe/watermelondb/decorators'
export default class UserIngredientDetail extends Model {
  static table = 'userIngredientdetails'
  static associations = {
    users: { type: 'belongs_to', key: 'user_id' },
    ingredientdetails: { type: 'belongs_to', key: 'ingredientdetail_id' },
    ingredientdetails: { type: 'belongs_to', key: 'ingredient_id' },
  }
  @field('user_id') user_id;
  @field('ingredient_id') ingredient_id;
  @field('ingredientdetail_id') ingredientdetail_id;
  @field('remark') remark;
  @field('count') count;
  @readonly @date('created_at') createdAt;

  @writer async deleteUserIngredientDetail(ingredient_id) {
    const useringredientdetails = await this.collections
    .get('userIngredientdetails')
    .query(Q.where('ingredient_id', ingredient_id))
    .fetch()

    try {
      await this.callWriter(() => useringredientdetails[0].destroyPermanently());
    } catch (e) {
      console.log(e);
    }
  }
}

By Following the online documents, this is how I access the delete method.

  handleDelete = async (ingredient_id) => {
    try {
      console.log("will be Deleted: ", ingredient_id)
      await UserIngredientDetail.deleteUserIngredientDetail(ingredient_id)
      setReload(true)
    } catch(e) {console.log(e)}
  }

Then the console error is shown below:

[TypeError: _useringredientdetail.default.deleteUserIngredientDetail is not a function (it is undefined)]

Any Idea?

Sannijiv commented 1 year ago

It's not clear for me either how to actually call the @writer methods on the model class so i'm also facing this issue.

carlos-cne commented 11 months ago

I agree with this issue. I've tried many ways to use my writer method, but it didn't work. I also found a question on the SO about this. I was trying to understand the issue and may be open to at least improving the docs, but I am with lack of knowledge.