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

Datastore Save errors out with an entity retrieved from Datastore Get #1730

Closed
paritosh16 opened this issue Oct 20, 2016 · 3 comments
Closed
Assignees
Labels
api: datastore Issues related to the Datastore API.

Comments

@paritosh16
Copy link

Environment Details

  • OS Details - Mac OSX 10.12
  • Node version - 4.3.0
  • npm version - 3.10.3
  • google-cloud/datastore version - 0.5.0

Steps to reproduce

Trying to update an entity in datastore. This documentation says that I should get an entity, make the required changes and then save it back using the save method.

var Datastore = require('@google-cloud/datastore');
//Initialize the Datastore object.
var ds = Datastore({
  projectId: 'my-project'
});
//Datastore Key
var key = ds.key({
  namespace: 'myNameSpace',
  path: ['kind', 'name_of_the_entity']
});
ds.get(key, function(error, entity) {
  if(error) {
    //Error handler
  } else {
    //Make changes in entity.
    ds.save(entity, function(error) {
      if(error) {
        //Error handler
      } else {
        //Entity updated.
      }
    })
  }
});

Stack Trace

/project/node_modules/@google-cloud/datastore/src/entity.js:506
  if (!is.string(key.path[0])) {
                    ^

TypeError: Cannot read property 'path' of undefined
    at Object.keyToKeyProto (/project/node_modules/@google-cloud/datastore/src/entity.js:506:21)
    at Object.isKeyComplete (/project/node_modules/@google-cloud/datastore/src/entity.js:434:32)
    at /project/node_modules/@google-cloud/datastore/src/request.js:869:17
    at Array.forEach (native)
    at DatastoreRequest.save (/project/node_modules/@google-cloud/datastore/src/request.js:854:12)
at Datastore.wrapper [as save] (/project/node_modules/@google-cloud/datastore/node_modules/@google-cloud    /common/src/util.js:654:29)
    at /project/index.js:72:29
    at /project/node_modules/@google-cloud/datastore/src/request.js:417:7
at ConcatStream.<anonymous> (/project/node_modules/@google-cloud/datastore/node_modules/concat-stream/    index.js:36:43)
    at emitNone (events.js:72:20)

I would need the save method to work for all the update operations happening across my application

@stephenplusplus
Copy link
Contributor

We changed how entities are returned in our last release, so that the key is accessible through a property on the entity:

ds.get(key, function(error, entity) {
  // Before:
  entity.key = Key object
  entity.data = {...data...}

  // After:
  entity = {...data...}
  entity[ds.KEY] = Key object
});

So your example should work with some changes to account for this:

ds.get(key, function(error, entity) {
  if(error) {
    //Error handler
  } else {
    //Make changes in entity.
    ds.save({
      key: entity[ds.key],
      data: entity
    }, function(error) {
      if(error) {
        //Error handler
      } else {
        //Entity updated.
      }
    })
  }
});

Sorry for the confusion. Our docs were left behind in some spots, but I've sent #1725 to correct them. Can you let me know where you found this example in the docs so I can make sure I got it in #1725?

@stephenplusplus stephenplusplus added the api: datastore Issues related to the Datastore API. label Oct 20, 2016
@paritosh16
Copy link
Author

Thank you @stephenplusplus for the prompt reply. The second last code snippet under examples in get method documentation illustrates how to save an entity using the get and the save method.

@stephenplusplus
Copy link
Contributor

Ah, ha! Thanks for calling that out. I've sent a PR to fix that, and I believe after that, we'll have them all covered. Sorry again for the inconvenience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API.
Projects
None yet
Development

No branches or pull requests

2 participants