Skip to content

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

Description

@paritosh16

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

Activity

  1. stephenplusplus commented on Oct 20, 2016

    @stephenplusplus
    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?

  2. paritosh16 commented on Oct 20, 2016

    @paritosh16
    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.

  3. stephenplusplus commented on Oct 21, 2016

    @stephenplusplus
    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

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions