Aws-mobile-appsync-sdk-js: "Network error: Can't find field <queryName> on object undefined" in NodeJS

Created on 9 Aug 2019  路  6Comments  路  Source: awslabs/aws-mobile-appsync-sdk-js

Do you want to request a feature or report a bug?
bug

What is the current behavior?
I am trying to access AppSync data from NodeJS. I am following the instructions at:

https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-node.html#create-a-client-application

and

https://aws-amplify.github.io/docs/js/api#aws-appsync-sdk

I get the following error 100% of the time: "Network error: Can't find field <queryName> on object undefined."

The setup works just fine if I set disableOffline to true in the client initialization.

Related to https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/102, but seeing the error consistently on NodeJS unlike in the other case where it is sporadic.

to-be-reproduced appsync-core

Most helpful comment

I came across this error recently, albeit I am using this lib in a React Native app and not Node.

I am able to reproduce this by having an error thrown in the jwtToken method that is provided to the AppSync client when it is initialized (see this section of the README and look for the commented // jwtToken line).

const client = new AWSAppSyncClient({
  // other config options omitted for brevity
  auth: {
    type: AppSyncConfig.authenticationType,
    jwtToken: async () => {
      throw new Error('Throwing an error inside jwtToken')
    },
  }
})

This caused downstream issues in the library and for redux-offline (which is used internally by the aws appsync js sdk). If my client code is attempting to perform a mutation and the jwtToken method throws, the mutation is added to redux-offline's queue, and redux-offline's store sets a busy value to true. It seems like this busy value never toggles back to false, so the optimistic response is never applied to the cache since the mutation is "stuck" in the offline queue. Since it gets "stuck" there, when I try to perform a query to read a value from the cache that assumes the optimistic update has been applied, I get the same "Can't find field on object undefined" error.

This may not be relevant at all to your situation, but I figured I'd at least leave this comment for future GitHub issue lurkers who might be encountering this bug.

All 6 comments

I came across this error recently, albeit I am using this lib in a React Native app and not Node.

I am able to reproduce this by having an error thrown in the jwtToken method that is provided to the AppSync client when it is initialized (see this section of the README and look for the commented // jwtToken line).

const client = new AWSAppSyncClient({
  // other config options omitted for brevity
  auth: {
    type: AppSyncConfig.authenticationType,
    jwtToken: async () => {
      throw new Error('Throwing an error inside jwtToken')
    },
  }
})

This caused downstream issues in the library and for redux-offline (which is used internally by the aws appsync js sdk). If my client code is attempting to perform a mutation and the jwtToken method throws, the mutation is added to redux-offline's queue, and redux-offline's store sets a busy value to true. It seems like this busy value never toggles back to false, so the optimistic response is never applied to the cache since the mutation is "stuck" in the offline queue. Since it gets "stuck" there, when I try to perform a query to read a value from the cache that assumes the optimistic update has been applied, I get the same "Can't find field on object undefined" error.

This may not be relevant at all to your situation, but I figured I'd at least leave this comment for future GitHub issue lurkers who might be encountering this bug.

I have encounter this error when I have expired credentials.
client = new AWSAppSyncClient({ url: this.host, region: aws.region, auth: { type: AUTH_TYPE.AWS_IAM, credentials: (): any => (AWS.config.credentials), }, });
@matthamil's answer applies in my case.

I experienced the same error.

Node requires a polyfill to support offline storage for the libraries in the example. Prepend your code with the following:

// POLYFILLS
global.WebSocket = require('ws');
global.window = global.window || {
    setTimeout: setTimeout,
    clearTimeout: clearTimeout,
    WebSocket: global.WebSocket,
    ArrayBuffer: global.ArrayBuffer,
    addEventListener: function () { },
    navigator: { onLine: true },
    localStorage: {
        store: {},
        getItem: function (key) {
            return this.store[key]
        },
        setItem: function (key, value) {
            this.store[key] = value
        },
        removeItem: function (key) {
            delete this.store[key]
        }
    }
};

Credit to https://thatcoder.space/aws-appsync-example-for-query-using-nodejs/ for the pointers here.

I got that error messages when using yarn instead of npm, what are you using @krsnaa ?

I've searched for a while for this issue and people say that 'set disableOffline true' but what if I want to use offline feature???????????????

Does anybody know the solution not setting disableOffline: true......???

@benmac3 doesn't that solution turn off offline feature by clearing localStorage everytime the app starts? Then what's the difference from setting disableOffline: true?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

e-yoshi picture e-yoshi  路  3Comments

ArronHsiao picture ArronHsiao  路  3Comments

AlexThomas90210 picture AlexThomas90210  路  3Comments

jd-carroll picture jd-carroll  路  3Comments

ciocan picture ciocan  路  4Comments