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

Error saving post TypeError: Cannot read properties of undefined (reading 'version') #9470

Closed
3 tasks done
goldwareJapan opened this issue Jan 12, 2022 · 12 comments
Closed
3 tasks done
Assignees
Labels
DataStore Related to DataStore category

Comments

@goldwareJapan
Copy link

goldwareJapan commented Jan 12, 2022

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

DataStore

Amplify Categories

storage

Environment information

  System:
    OS: macOS 12.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 20.88 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.1 - ~/.nvm/versions/node/v12.22.1/bin/node
    Yarn: 1.22.17 - ~/.yvm/shim/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v12.22.1/bin/npm
    Watchman: 2021.10.18.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 97.0.4692.71
    Firefox: 95.0.2
    Safari: 15.2
  npmGlobalPackages:
    @aws-amplify/cli: 7.6.8
    @nestjs/cli: 8.1.5
    aws-cdk: 2.3.0
    expo-cli: 4.12.10
    npm: 8.3.0
    nx: 13.2.3
    pm2: 5.1.2
    prisma: 3.5.0
    typescript: 4.5.2



Describe the bug

DataStore.Query not working.

It was working at the office. I walked home and it gives this error.

Error saving post TypeError: Cannot read properties of undefined (reading 'version')

Screen Shot 2022-01-12 at 19 45 16

schema is

type Project @model @auth(rules:  [
    # Admin users can access any operation.
    { allow: groups, groups: ["Admin"] }

    # Each record may specify which groups may read them.
    { allow: groups, groupsField: "company", operations: [read] }
  ]) {
  id: ID!
  name: String
  status: ProjectStatus
  company: String
}

code is

         <Button
            type="primary"
            onClick={async () => {
              console.log('This is sparta 2');
              try {
                const projects = await DataStore.query(Project);
                console.log(
                  'Posts retrieved successfully!',
                  projects.length,
                  JSON.stringify(projects, null, 2),
                );
              } catch (error) {
                console.log('Error retrieving projects', error);
              }
            }}>
            load
          </Button>

I logged in as a admin user

no network request

my indexDB is empty after clicking button ( and error ).

Screen Shot 2022-01-12 at 20 07 44

Expected behavior

To load data.

@goldwareJapan
Copy link
Author

goldwareJapan commented Jan 12, 2022

OK. looks like this waas the reason.

Why are there 2 other libraries with the same export?

Now my problem is

At first it worked with import { DataStore } from 'aws-amplify';. created some data on cloud. then suddenly this gives error.

Changed to thisimport { DataStore } from '@aws-amplify/datastore';. no error, but no sync. Now I can't delete cloud data.

[REDACTED]

@goldwareJapan
Copy link
Author

goldwareJapan commented Jan 12, 2022

Now stuck with datastore does not sync with @auth problem.

come on. [REDACTED]

The project has a deadline and spends 4 hours ( almost every day) on broken amplify.

@goldwareJapan
Copy link
Author

goldwareJapan commented Jan 12, 2022

This at least sends network requests. But fails Unauthorized

enum ProjectStatus {
  OPEN
  ONGOING
  ARCHIVED
  CLOSED
}

type Project1 @model  {
  id: ID!
  name: String
  status: ProjectStatus
  # company: String
}

type Door @model  {
  id: ID!
  name: String!
}

But this does not even send a network request

enum ProjectStatus {
  OPEN
  ONGOING
  ARCHIVED
  CLOSED
}

type Project1 @model @auth(rules:  [ { allow: public } ]) {
  id: ID!
  name: String
  status: ProjectStatus
  # company: String
}

type Door @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
}

@chrisbonifacio
Copy link
Member

chrisbonifacio commented Jan 12, 2022

Hey @goldwareJapan 👋 Sorry you're experiencing issues.

I would recommend sticking with the DataStore export from the aws-amplify package unless you only need to use DataStore and no other amplify categories.

Can you share the contents of the cli-inputs.json file within the amplify/backend/api/<projectName> folder?

Also, if you can share the contents of your package.json file at the root of your project that would help as well.

Lastly, please share the response from AppSync in the network tab that includes the Unauthorized error. Just want to see what the full error message says to better understand what might need to be adjusted.

Thank you!

@chrisbonifacio chrisbonifacio self-assigned this Jan 12, 2022
@chrisbonifacio chrisbonifacio added DataStore Related to DataStore category pending-triage Issue is pending triage pending-response and removed pending-triage Issue is pending triage labels Jan 12, 2022
@chrisbonifacio
Copy link
Member

chrisbonifacio commented Jan 12, 2022

One thing I noticed is that at first your schema was using User Pool groups to authorize access to Project records but in the latest schema it looks like it's been changed to public access.

By default DataStore will try to use the default auth type configured for the AppSync resource, defined in the aws-exports.js file like so:

ex.

// aws-exports.js
"aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",

So - if the model is only authorized for public access, but the AppSync authentication type is set to Cognito User Pools, DataStore will try to make the query to AppSync with an auth mode of Cognito User Pools and that will return an Unauthorized error. That might explain why you're experiencing that issue.

You'd either have to log out to be able to query those records or you can adjust the auth rule to something like this in order to grant authenticated users access:

type Project1 @model @auth(rules:  [ { allow: public }, { allow: private } ]) {
  id: ID!
  name: String
  status: ProjectStatus
  # company: String
}

Now, in this example there are two types of authorization. Public and Private. Like I said before, by default DataStore will always try to use the default auth type. In order for DataStore to be able to use multiple auth types defined on a model, Multi Auth needs to be enabled.

We can do this by setting up our Amplify & DataStore configuration like so:

import { Amplify, AuthModeStrategyType } from 'aws-amplify';
import awsconfig from './aws-exports'; 

Amplify.configure({
  ...awsconfig,
  DataStore: {
    authModeStrategyType: AuthModeStrategyType.MULTI_AUTH
  }
})

More information on configuring auth rules for DataStore here: https://docs.amplify.aws/lib/datastore/setup-auth-rules/q/platform/js/#configure-multiple-authorization-types

@sammartinez
Copy link
Contributor

Hey @goldwareJapan, please take a look when you can on the above that @chrisbonifacio provided to help with your issue. Apologizes as well that you are having issues with the product but we are here to help the best we can. I do want to state that I did have to redact some of the statements above as we follow a Code of Conduct when providing issues to us. Please let us know if the above helps your case or how we can help further. Thanks ahead of time

@goldwareJapan
Copy link
Author

goldwareJapan commented Jan 13, 2022

@sammartinez sorry for the bad language. Like I said it was close to the deadline and this happened. We agree with your redact. it was irrelevant to the issue.

@chrisbonifacio Thank you for the advice. But we were in hurry so We decided to get data by the script and manually upload it after deleting the whole infrastructure and creating it from scratch. Luckily we had less than a thousand data. Currently, we are in the middle of the process.

This time we will avoid Datastore. In long term amplify too. maybe CDK or firebase. Not just because of this issue. Overall amplify have too many basic issues. And looks like all project in AMAZON except amplify admin console is not using it. Maybe because amplify is not production-ready. ( will create an issue about it later )

So you can close this issue.

@kathigeiger42
Copy link

Can we please reopen this. I am experiencing the same error. I am currently trying to create a react-native-windows app from an existing React Native app, which uses Amplify as a backend. One day, the Datastore connection was working, the next, I get the same error message as stated above. Help is urgently needed. I tried the multi-auth solution but it didn't help.

@kathigeiger42
Copy link

Can we please reopen this. I am experiencing the same error. I am currently trying to create a react-native-windows app from an existing React Native app, which uses Amplify as a backend. One day, the Datastore connection was working, the next, I get the same error message as stated above. Help is urgently needed. I tried the multi-auth solution but it didn't help.

Okay, so deleting the @aws-amplify/datastore npm package solved the issue.

@entest-hai
Copy link

I experience the same error

@chrisbonifacio
Copy link
Member

chrisbonifacio commented Apr 13, 2022

@entest-hai please open a new issue and fill out the bug report form with your environment info, reproduction steps, code snippets, etc. The cause of this issue could be different for each project.

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server amplify-help forum.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
DataStore Related to DataStore category
Projects
None yet
Development

No branches or pull requests

5 participants