Coder Social home page Coder Social logo

Records not saved properly about watermelondb HOT 8 CLOSED

nozbe avatar nozbe commented on May 22, 2024
Records not saved properly

from watermelondb.

Comments (8)

radex avatar radex commented on May 22, 2024 1

when you do:

const acc = await this.props.database.collections.get('accounts').create(account => {
   account.name = this.state.name;
   account.initialBalance = 0.0;
   account.currency = this.state.currency;
})

try console.log(acc._raw). Does it say what you'd expect?

from watermelondb.

alex-min avatar alex-min commented on May 22, 2024

that's a good idea, I have not thought about that. No it's full of empty values.
I've even tried to give static values to it to test but it did not work.

this code:

    let acc = await this.props.database.collections.get('accounts').create(account => {
      account.name = 'test';
      account.initialBalance = 0.1;
      account.currency = 'currency';
    });
    console.log(acc._raw);

is producing this log:

10-22 22:21:00.711 16572 18068 I ReactNativeJS: { id: '4zup0vsrn0lxklue',
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   _status: 'created',
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   _changed: '',
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   last_modified: null,
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   name: '',
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   initial_balance: 0,
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   computed_balance: 0,
10-22 22:21:00.711 16572 18068 I ReactNativeJS:   currency: '' }

That's what I don't get, the values are obviously recorded since on the view with the observable it works but because it's not saved properly, as soon as I reload the app I just get blank records.

from watermelondb.

radex avatar radex commented on May 22, 2024

OK, I think I know what this is. I think you have misconfigured decorators support in your Babel config file. When you set account.name = 'x', you literally set account.name instead of calling the utter that will update account._raw.name

Have you gone through Installation guide step by step? Do you have the "legacy" option for decorators set to true?

from watermelondb.

alex-min avatar alex-min commented on May 22, 2024

Yes indeed you were right, I have something wrong setup in the babelrc, I close the issue :)

Thanks a lot! :)

from watermelondb.

rasanu avatar rasanu commented on May 22, 2024

@alex-min facing same issue can you show me your codes

from watermelondb.

alex-min avatar alex-min commented on May 22, 2024

Please see #207 (comment)

I had

{
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
    ["@babel/plugin-transform-flow-strip-types"],
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

instead of

{
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-transform-runtime", {
      "helpers": true,
      "regenerator": false
    }]
  ]
}

in my babelrc.

from watermelondb.

kesha-antonov avatar kesha-antonov commented on May 22, 2024

I had this issue too.

Need to move export default to bottom of file:

import { Model } from '@nozbe/watermelondb'
import { field } from '@nozbe/watermelondb/decorators'

export default class User extends Model {
  static table = 'users'

  @field('email') email
  @field('phone') phone
  @field('last_name') lastName
  @field('first_name') firstName
  @field('middle_name') middleName
}

==>

import { Model } from '@nozbe/watermelondb'
import { field } from '@nozbe/watermelondb/decorators'

class User extends Model {
  static table = 'users'

  @field('email') email
  @field('phone') phone
  @field('last_name') lastName
  @field('first_name') firstName
  @field('middle_name') middleName
}

export default User

from watermelondb.

amhed avatar amhed commented on May 22, 2024

I'm also running into this.

schema

import { appSchema, tableSchema } from '@nozbe/watermelondb';

export const schema = appSchema({
  version: 5,
  tables: [
    tableSchema({
      name: 'wallet',
      columns: [
        { name: 'walletId', type: 'string', isIndexed: true },
        { name: 'currencyCodeStr', type: 'string', isIndexed: true },
      ],
    }),
  ],
});

Model

import { Model } from '@nozbe/watermelondb';
import { field } from '@nozbe/watermelondb/decorators';

export class WalletWMO extends Model {
  static table = 'wallet';

  @field('walletId')
  walletId!: string;

  @field('currencyCodeStr')
  currencyCodeStr!: string;
}

Code to insert

database.write(async () => {
  const table = database.get<WalletWMO>('wallet');
  const result = await table.create((record: WalletWMO) => {
    record.walletId = this.id;
    record.currencyCodeStr = this.currencyCode.rawValue;
  };);
});

And yet, the underlying `_raw values are empty/default

image

Tsconfig has

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

BabelConfig has

const plugins = [
    'react-hot-loader/babel',
    ['@babel/plugin-proposal-private-methods', { loose: true }],
    ['@babel/plugin-proposal-private-property-in-object', { loose: true }],
    ['@babel/plugin-proposal-class-properties', { loose: true }],
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    [
      '@babel/plugin-transform-runtime',
      {
        helpers: true,
        regenerator: true,
      },
    ],
]

from watermelondb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.