aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

TypeError: Cannot read property 'isReadOnly' of undefined #13823

Closed Cristi6 closed 2 months ago

Cristi6 commented 2 months ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

DataStore

Amplify Version

v6

Amplify Categories

No response

Backend

Amplify Gen 2 (Preview)

Environment information

``` # Put output below this line System: OS: macOS 14.3 CPU: (12) arm64 Apple M3 Pro Memory: 421.78 MB / 36.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node Yarn: 4.4.1 - ~/.nvm/versions/node/v20.12.2/bin/yarn npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm pnpm: 9.10.0 - ~/.nvm/versions/node/v20.12.2/bin/pnpm Watchman: 2024.05.06.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 128.0.6613.138 Safari: 17.3 npmPackages: %name%: 0.1.0 @aws-amplify/backend: ^1.2.1 => 1.2.1 @aws-amplify/backend-cli: ^1.2.6 => 1.2.6 @aws-amplify/react-native: ^1.1.5 => 1.1.5 @aws-amplify/rtn-web-browser: ^1.1.0 => 1.1.0 @babel/core: ^7.20.0 => 7.25.2 @babel/preset-env: ^7.20.0 => 7.25.4 @babel/runtime: ^7.20.0 => 7.25.6 @react-native-async-storage/async-storage: ^2.0.0 => 2.0.0 @react-native-community/netinfo: ^11.4.0 => 11.4.0 @react-native/babel-preset: 0.75.3 => 0.75.3 @react-native/eslint-config: 0.75.3 => 0.75.3 @react-native/metro-config: 0.75.3 => 0.75.3 @react-native/typescript-config: 0.75.3 => 0.75.3 @react-navigation/native: ^6.1.18 => 6.1.18 @react-navigation/native-stack: ^6.11.0 => 6.11.0 @sentry/react-native: ^5.32.0 => 5.32.0 @types/react: ^18.2.6 => 18.3.5 @types/react-native-get-random-values: ^1 => 1.8.2 @types/react-test-renderer: ^18.0.0 => 18.3.0 aws-amplify: ^6.6.0 => 6.6.0 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/data: undefined () aws-amplify/data/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () aws-cdk: ^2 => 2.158.0 aws-cdk-lib: ^2 => 2.158.0 babel-jest: ^29.6.3 => 29.7.0 babel-plugin-module-resolver: ^5.0.2 => 5.0.2 constructs: ^10.0.0 => 10.3.0 dayjs: ^1.11.13 => 1.11.13 esbuild: ^0.23.1 => 0.23.1 eslint: ^8.19.0 => 8.57.0 eslint-plugin-import: ^2.30.0 => 2.30.0 eslint-plugin-unused-imports: ^4.1.4 => 4.1.4 i18next: ^23.15.1 => 23.15.1 intl-pluralrules: ^2.0.1 => 2.0.1 jest: ^29.6.3 => 29.7.0 prettier: 2.8.8 => 2.8.8 (2.3.2, 1.19.1) react: 18.3.1 => 18.3.1 react-error-boundary: ^4.0.13 => 4.0.13 react-hook-form: ^7.53.0 => 7.53.0 react-i18next: ^15.0.1 => 15.0.1 react-native: 0.75.3 => 0.75.3 react-native-config: ^1.5.3 => 1.5.3 react-native-get-random-values: ^1.11.0 => 1.11.0 react-native-safe-area-context: ^4.11.0 => 4.11.0 react-native-screens: ^3.34.0 => 3.34.0 react-native-svg: ^15.6.0 => 15.6.0 react-native-toast-message: ^2.2.0 => 2.2.0 react-native-unistyles: ^2.9.2 => 2.9.2 react-native-url-polyfill: ^2.0.0 => 2.0.0 react-test-renderer: 18.3.1 => 18.3.1 tsx: ^4.19.1 => 4.19.1 typescript: ^5.0.0 => 5.6.2 (4.4.4, 4.9.5) zustand: ^4.5.5 => 4.5.5 npmGlobalPackages: corepack: 0.25.2 firebase-tools: 13.8.3 npm: 10.5.0 ```

Describe the bug

Hi,

I just started experimenting with AWS Amplify and I'm struggling with an error about authorizing an update in DynamoDB.

Expected behavior

I was expecting a successful update.

Reproduction steps

So, my defined backend looks like this:

const schema = a.schema({
  Users: a
    .model({
      userId: a.string().required(),
      email: a.email().required(),
      name: a.string().required().default(''),
      hasSubscription: a.boolean().required().default(false),
      hasFinishedOnboarding: a.boolean().required().default(false),
      signInMethod: a.string().required().default('anonymous'),
    })
    .identifier(['userId'])
    .authorization(allow => [allow.owner()]),
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: 'userPool',
  },
});

Regarding auth, I configured only sign in with Google, which works and I am able to create a user object in DynamoDB. As a second step I was trying to update a field in the object using:

try {
      const client = generateClient<Schema>();
      const updatedUser: User = { ...user, name: newName };
      const { data, errors: updateErrors } = await client.models.Users.update(updatedUser);
} catch (error) {
      console.log(error)
}

The user object has all the fields according to the schema + the owner field, as specified in the documentation as well. When calling the above I catch the error: TypeError: Cannot read property 'isReadOnly' of undefined]

I started debugging and found the issue. Inside APIClient.ts there is this code:

    case 'UPDATE':
      // readonly fields are not  updated
      variables = {
        input: arg
          ? Object.fromEntries(
              Object.entries(
                normalizeMutationInput(
                  arg,
                  modelDefinition,
                  modelIntrospection,
                ),
              ).filter(([fieldName]) => {
                const { isReadOnly } = fields[fieldName];

                return !isReadOnly;
              }),
            )
          : {},
      };
      break;

It seems the issue is that the arg object has all the fields from the schema + the owner field, but the fields object has only the fields from the schema. So when the filter function is called for each element, it reaches the point where it tries to access fields.owner.isReadOnly, where fields.owner is undefined because owner is not part of the schema.

So, am I doing something wrong? Have I missed something in the documentation or is it an issue?

If you need more details, let me know.

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

Cristi6 commented 2 months ago

Well I figured to remove the owner field received in the response from the create request. 🤦