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 get with async/await returns array?? #2749

Closed
ghost opened this issue Nov 18, 2017 · 6 comments
Closed

DataStore get with async/await returns array?? #2749

ghost opened this issue Nov 18, 2017 · 6 comments
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.

Comments

@ghost
Copy link

ghost commented Nov 18, 2017

When using await to call DataStore.get([klind,key]) the return value is always an array..

Event when the record was not found, the undefined value also comes back as an array of [ undefined ]

But when I use standard async calls, it works fine.,..

Environment details

  • OS: OSX 10.12.6
  • Node.js version: v8.7.0
  • npm version: 5.5.1
  • google-cloud-node version: 1.1.0

Steps to reproduce

  1. require google-cloud
  2. const datastore = Datastore({ projectId: projectId});
  3. var itemkey = datastore.key([my_kind, my_key]);
  4. var itementry = await datastore.get(itemkey);

For items found it always returned as an array [ object ] or when record doesnt exisit and not found, it always returns [ undefined ]

@stephenplusplus stephenplusplus added api: datastore Issues related to the Datastore API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Nov 18, 2017
@stephenplusplus
Copy link
Contributor

This seems a little tricky. If you call get(key) and it's not an entity, I would expect the error handler to be called with some sort of 404 error.

But what if you call get([keyExists, keyDoesNotExist])? The system so far has been somewhat best-effort, so that the ones that do exist can come through.

What do you think would be a sane way to handle this?

cc @callmehiphop

@ghost
Copy link
Author

ghost commented Nov 23, 2017

the problem is not the "not found" cases..

the problem is that the return value when using async/await is always an array.. but with normal get(key).then().else(); the returned objects are entities and not found is undefined..

but foo = await get(key); returns array.. i.e. [ entity ] and when not found [ undefined ]

so the work around is to check if return value is not undefined, then check if it has a count array elements more than 0.. if it does, then get the first element.. then evaluate if its entity or undefined..

its a bug for async/await

@stephenplusplus
Copy link
Contributor

Do you have a way we could solve this?

cc @callmehiphop

@stephenplusplus stephenplusplus removed the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label Nov 27, 2017
@callmehiphop
Copy link
Contributor

This is tricky because the upstream API is not returning a 404 error here. But I would handle it like this

const [entity] = await datastore.get(key);

if (!entity) {
  throw new Error('No entities found!');
}

@stephenplusplus stephenplusplus added the type: question Request for information or clarification. Not an issue. label Nov 27, 2017
@stephenplusplus
Copy link
Contributor

This is starting to sound like this issue: #2556

@kdupreez I'm not sure we can solve what you're asking for. Please elaborate if you have any solutions in mind and I will re-open. Otherwise, #2556 might be the real solution you're looking for.

@ghost
Copy link
Author

ghost commented Dec 12, 2017

No its not.. promises work fine.. its specifically Async/Await..

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: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants