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

Errors with entity backend methods #210

Open
alexkreidler opened this issue Jun 28, 2023 · 1 comment
Open

Errors with entity backend methods #210

alexkreidler opened this issue Jun 28, 2023 · 1 comment

Comments

@alexkreidler
Copy link

alexkreidler commented Jun 28, 2023

Describe the bug
There are two errors that happen when creating a backend method.

To Reproduce
I have some code like this:

import { BackendMethod, Entity, EntityBase, Fields } from 'remult';

@Entity('page_group', { allowApiCrud: true })
export class PageGroup extends EntityBase {
  @Fields.uuid()
  id!: string;

  @Fields.date()
  created_at!: Date;

  @Fields.date()
  updated_at!: Date;

  @Fields.uuid()
  owner_id!: string;

  @Fields.string()
  name = '';

  @Fields.string()
  frequency = 'once';

  @Fields.boolean()
  js_rendered = true;

  @BackendMethod({ allowed: true })
  toggleCompleted() {
    // this.frequency = this.frequency === 'once' ? 'daily' : 'once'
    // await this.save()
  }
}

I originally had the commented out code active, and then tested with it commented out. I then got the first error shown below when sending a JSON object like { "args": [] } or { "args": ["string"] }, and the second error when removing args and sending an empty object {} or no body.

server started at http://0.0.0.0:3000
sqlite does not support inserting default values. Set the `useNullAsDefault` flag to hide this warning. (see docs https://knexjs.org/guide/query-builder.html#insert).
Ensure Schema: 22.764ms
{
  message: "Cannot read properties of undefined (reading 'isNewRow')",
  stack: [
    "TypeError: Cannot read properties of undefined (reading 'isNewRow')",
    '    at Object.<anonymous> (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/remult@0.21.1/node_modules/remult/src/server-action.js:285:72)',
    '    at step (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:195:27)',
    '    at Object.next (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:176:57)',
    '    at fulfilled (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:166:62)'
  ],
  url: '/api/page_group/toggleCompleted',
  method: 'POST'
}
/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/remult@0.21.1/node_modules/remult/src/server-action.js:266
                                    d.args = d.args.map(function (x) { return isCustomUndefined(x) ? undefined : x; });
                                                    ^
TypeError: Cannot read properties of undefined (reading 'map')
    at Object.<anonymous> (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/remult@0.21.1/node_modules/remult/src/server-action.js:266:53)
    at step (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:195:27)
    at Object.next (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:176:57)
    at /home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:169:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:165:16)
    at /home/alex/c2/magiscrape/backend2/node_modules/.pnpm/remult@0.21.1/node_modules/remult/src/server-action.js:260:257
    at RemultServerImplementation.<anonymous> (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/remult@0.21.1/node_modules/remult/server/expressBridge.js:655:58)
    at step (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:195:27)
    at Object.next (/home/alex/c2/magiscrape/backend2/node_modules/.pnpm/tslib@2.6.0/node_modules/tslib/tslib.js:176:57)
 ELIFECYCLE  Command failed with exit code 1.

Expected behavior
The function should be called without any problems

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • Browser: Firefox
  • Version: ^0.21.1
@yoni-rapoport
Copy link
Collaborator

As opposed to static BackendMethods, instance BackendMethods (such as this one) require additional entity instance data in the request. In this case, the JSON object you send should look like this:

{
    "rowInfo": {
        "data": { },
        "isNewRow": false,
        "wasChanged": false,
        "id": "<THE-UUID>"
    }
}

This is unfortunately not documented in the OpenAPI spec exported by Remult so please leave the issue open so we can fix that.

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

2 participants