amazon-archives / amazon-cognito-js

Amazon Cognito Sync Manager for JavaScript
http://aws.amazon.com/cognito
Apache License 2.0
200 stars 83 forks source link

CognitoSyncDataset.get(key, callback) returns "undefined" value. #51

Open dap129 opened 7 years ago

dap129 commented 7 years ago

After logging in and getting authenticated credentials for a user I want to create/read datasets for the user in cognito identity pool. I am able to create and read datasets from identity pool after which the records are getting stored in the browser's localStorage. But using 'get' and 'put' methods of "CognitoSyncDataset" in 'amazon-cognito-js' sdk, I am getting values for 'put' but getting "undefined" for get even though it is there in the localStorage. Here's my code:

var syncClient = new AWS.CognitoSyncManager();
syncClient.openOrCreateDataset("myDataset", function(err, dataset) {
              dataset.get("testk1", function(err, value) {
                if(err) console.log(err);
                else{
                  console.log("Record got:\n");
                  console.log(value);
                }
              });
              dataset.synchronize({
                onSuccess: function(dataset, newRecords) {
                  console.log('Successfully synchronized ' + newRecords.length + ' new records.');
                },
                onFailure: function(err) {
                  console.log('Synchronization failed.');
                  console.log(err);
                },
                onConflict: function(dataset, conflicts, callback) {
                  var resolved = [];
                  for (var i=0; i<conflicts.length; i++) {
                    // Take remote version.
                    // resolved.push(conflicts[i].resolveWithRemoteRecord());
                    // Or... take local version.
                    resolved.push(conflicts[i].resolveWithLocalRecord());

                    // Or... use custom logic.
                    // var newValue = conflicts[i].getRemoteRecord().getValue() + conflicts[i].getLocalRecord().getValue();
                    // resolved.push(conflicts[i].resovleWithValue(newValue);
                  }
                  dataset.resolve(resolved, function() {
                    return callback(true);
                  });
                  // Or... callback false to stop the synchronization process.
                  // return callback(false);
                }  
              });
            });
tqhoughton commented 7 years ago

I am also getting this error.

EDIT: After some more testing it appears that this is only an issue when attempting to retrieve datasets that were created in the AWS console. I ran some code that created a dataset, then tried getting it and it worked. Reloading the AWS console shows the newly created dataset as well. So I think this may be not be an issue unique to amazon-cognito-js but one that is linked to how the AWS console creates datasets.