Friday, 7 June 2019

Google Datastore can't update an entity

I'm having issues retrieving an entity from Google Datastore. Here's my code:

    async function pushTaskIdToCurrentSession(taskId){

    console.log(`Attempting to add ${taskId} to current Session: ${cloudDataStoreCurrentSession}`);

    const transaction = datastore.transaction();
    const taskKey = datastore.key(['Session', cloudDataStoreCurrentSession]);

    try {
      await transaction.run();

      const [task] = await transaction.get(taskKey);
      let sessionTasks = task.session_tasks;
      sessionTasks.push(taskId);
      task.session_tasks = sessionTasks;

      transaction.save({
        key: taskKey,
        data: task,
      });
      transaction.commit();
      console.log(`Task ${taskId} added to current Session successfully.`);

    } catch (err) {
        console.error('ERROR:', err);
        transaction.rollback();
    }
}

taskId is a string id of another entity that I want to store in an array of a property called session_tasks.

But it doesn't get that far. After this line:

const [task] = await transaction.get(taskKey);

The error is that task is undefined: ERROR: TypeError: Cannot read property 'session_tasks' of undefined at pushTaskIdToCurrentSession

Anything immediately obvious from this code?

UPDATE:

Using this instead: const task = await transaction.get(taskKey).catch(console.error); Gets me a task object, but it seems to be creating a new entity on the datastore: datastore entity error

I also get this error:

(node:19936) UnhandledPromiseRejectionWarning: Error: Unsupported field value, undefined, was provided. at Object.encodeValue (/Users/.../node_modules/@google-cloud/datastore/build/src/entity.js:387:15)

This suggests the array is unsupported?



from Google Datastore can't update an entity

No comments:

Post a Comment