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 query and get don't return entity data, only data object #1724

Closed
schliffer opened this issue Oct 19, 2016 · 8 comments
Closed

Datastore query and get don't return entity data, only data object #1724

schliffer opened this issue Oct 19, 2016 · 8 comments
Assignees
Labels
api: datastore Issues related to the Datastore API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@schliffer
Copy link

schliffer commented Oct 19, 2016

Environment details

  • OS: Goobuntu Linux/GCE
  • Node.js version: 4.6.0
  • npm version: 2.15.9
  • google-cloud-node version: 0.5.0

Steps to reproduce

Trying to use @google-cloud/datastore as part of a project. When querying the datastore, the data returned is only the data, and does not contain the key/id. For example the following code:

'use strict';

var Datastore = require('@google-cloud/datastore');

var ds = Datastore({
  projectId: 'my-project'
});

var dataservice = {};

dataservice.getFirstGame = function() {
  var query = ds.createQuery('Game').limit(1);
  ds.runQuery(query, (err, entities) => {
    console.log(err || entities);
  });
};

dataservice.getFirstGame();

Logs

[ { TeamTwo: [ 'player2' ],
    TeamOne: [ 'player1' ],
    LeagueId: 'somestring',
    GameResult: 1,
    DatePlayed: Tue Oct 18 2016 12:31:05 GMT-0700 (PDT) } ]

The documentation indicates that the entities should be an array of objects that each has the key and data object. We're not getting either of those, it looks like we're just getting an array of only the data objects.

We need the key/id in order to later update the object in the datastore.

Thanks!

@stephenplusplus
Copy link
Contributor

stephenplusplus commented Oct 19, 2016

Thanks for opening the issue. This was a recent change (after a very old feature request) that returns only the data, and lets the user grab the key from the data object itself using a symbol we provide. Don't bother re-reading that, here's an example:

dataservice.getFirstGame = function() {
  var query = ds.createQuery('Game').limit(1);
  ds.runQuery(query, (err, entities) => {
    console.log(err || entities);

    if (entities.length > 0) {
      var keys = entities.map(function(entity) {
        return entity[Datastore.KEY];
      });
    }
  });
};

dataservice.getFirstGame();

Sorry for the docs being wrong, I completely skipped over that section when making the PR. I'll send a fix for that now.

related: #1719

@stephenplusplus stephenplusplus added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. docs api: datastore Issues related to the Datastore API. labels Oct 19, 2016
@stephenplusplus stephenplusplus self-assigned this Oct 19, 2016
@schliffer
Copy link
Author

Wow, thank you for the super-fast response, this is exactly what I was looking for. Thanks!

@wiliame
Copy link

wiliame commented Oct 19, 2016

4 hours.... I just updated the library and it broke the project. Thanks!

@stephenplusplus
Copy link
Contributor

Sorry @wiliame! Check the release notes when upgrading: https://github.com/GoogleCloudPlatform/google-cloud-node/releases

We're working on adding these to the docs site for more visibility. Additionally, 1.0beta is on the horizon, so expect more stability from this API.

@wiliame
Copy link

wiliame commented Oct 19, 2016

@stephenplusplus I was searching for them, thanks. I upgraded to see if .offset stops returning empty results at v0.3.0. I will continue trying :)

@thevinci
Copy link

Why is this not part of the documentation? I had to spend a WHOLE day to find this.

@stephenplusplus
Copy link
Contributor

Sorry @thevinci, where is it missing / where should it be?

@thevinci
Copy link

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. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

4 participants