Watermelondb: ReferenceError: Can't find variable:field

Created on 19 May 2020  路  1Comment  路  Source: Nozbe/WatermelonDB

I am using this library for the first time. I followed all the installation steps. Even tried the Step 6 from the docs but I am not able to get the database up and running.

This is the error message:

WhatsApp Image 2020-05-20 at 3 24 06 AM

This is the code in my various files:

// index.js
import 'es6-symbol/implement';
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import {Database} from '@nozbe/watermelondb';
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';

import schema from './src/watermelon/model/schema';
import Message from './src/watermelon/model/Message';
import MessageChannel from './src/watermelon/model/MessageChannels';

// First, create the adapter to the underlying database:
const adapter = new SQLiteAdapter({
  schema,
});

// Then, make a Watermelon database from it!
const database = new Database({
  adapter,
  modelClasses: [MessageChannel, Message],
  actionsEnabled: true,
});

AppRegistry.registerComponent(appName, () => App);

// src/watermelon/model/Message.js

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

export default class Message extends Model {
  static table = 'messages';
  static associations = {
    message_channels: {type: 'belongs_to', foreignKey: 'channel_name'},
  };

  @field('message_id') id;
  @field('parent_id') parentId;
  @field('from') from;
  @field('to') to;
  @field('message_body') messageBody;
  @field('media') media;
  @field('read_status') readStatus;
  @field('is_forwarded') isForwarded;

  @date('timestamp_at') timestamp;
}


// src/watermelon/model/schema.js

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

export default appSchema({
  version: 1,
  tables: [
    tableSchema({
      name: 'message_channels',
      columns: [
        {name: 'channel_name', type: 'string', isIndexed: true},
        {name: 'last_message_id', type: 'string'},
      ],
    }),
    tableSchema({
      name: 'messages',
      columns: [
        {name: 'message_id', type: 'string', isIndexed: true},
        {name: 'parent_id', type: 'string', isIndexed: true, isOptional: true},
        {name: 'channel_name', type: 'string', isIndexed: true},
        {name: 'from', type: 'string'},
        {name: 'to', type: 'string'},
        {name: 'message_body', type: 'string', isOptional: true},
        {name: 'timestamp_at', type: 'string'},
        {name: 'media', type: 'string', isOptional: true},
        {name: 'read_status', type: 'string'},
        {name: 'is_forwarded', type: 'boolean'},
      ],
    }),
    // tableSchemas go here...
  ],
});


Most helpful comment

I think you are missing an import in the model file

import { field, date, children } from "@nozbe/watermelondb/decorators";

>All comments

I think you are missing an import in the model file

import { field, date, children } from "@nozbe/watermelondb/decorators";

Was this page helpful?
0 / 5 - 0 ratings