Home > Enterprise >  Unable to get record from GCP DataStore when following Google documentation
Unable to get record from GCP DataStore when following Google documentation

Time:10-05

I am trying to upsert a record in DataStore and retrieve it following Google's own documentation. Upsert works, but retrieval doesn't. Is the documentation outdated? What else could be wrong?

Error: 3 INVALID_ARGUMENT: Key path element must not be incomplete: [Task: ]

Upsert:

import { Datastore } from '@google-cloud/datastore'

const datastore = new Datastore()

const taskKey = datastore.key('Task');
const task = {
  category: 'Personal',
  done: false,
  priority: 4,
  description: 'Learn Cloud Datastore',
};

const entity = {
  key: taskKey,
  data: task,
};

await datastore.upsert(entity);
// Task inserted successfully.

Get:

import { Datastore } from '@google-cloud/datastore'

const datastore = new Datastore()

const taskKey = datastore.key('Task'); // error occurs here
const [entity] = await datastore.get(taskKey);

console.log(entity);

CodePudding user response:

The datastore.key('Task'); statement generates a random ID that you can check by logging the response of upsert() method. You must specify that ID when retrieving data as shown below:

const taskKey = datastore.key(['Task', 12345]); // <-- array here

await datastore.upsert(entity);

Similarly, you can fetch data by specifying the ID:

const data = await datastore.get(datastore.key(["Task", 12345]))

Replace the ID with your custom ID or the one returned by upsert(). Check the Datastore console for more information.

  • Related