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.
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
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?
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
jwtTokenmethod that is provided to the AppSync client when it is initialized (see this section of the README and look for the commented// jwtTokenline).This caused downstream issues in the library and for on object undefined" error.
redux-offline(which is used internally by the aws appsync js sdk). If my client code is attempting to perform a mutation and thejwtTokenmethod throws, the mutation is added toredux-offline's queue, andredux-offline's store sets abusyvalue totrue. It seems like thisbusyvalue never toggles back tofalse, 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 fieldThis 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.