0
votes

I am attempting to update an entity in my datastore kind using sample code from here https://cloud.google.com/datastore/docs/reference/libraries. The actual code is something like this:

/ Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Creates a client
const datastore = new Datastore({
  projectId: projectId,
});

// The kind for the new entity
const kind = 'Task';
// The name/ID for the new entity
const name = 'sampletask1';
// The Cloud Datastore key for the new entity
const taskKey = datastore.key([kind, name]);

// Prepares the new entity
const task = {
  key: taskKey,
  data: {
    description: 'Buy milk',
  },
};

// Saves the entity
datastore
  .save(task)
  .then(() => {
    console.log(`Saved ${task.key.name}: ${task.data.description}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

I tried to create a new entity using this code. But when I ran this code and checked the datastore console, there were no entitites created.Also, I am unable to update an existing entity. What could be the reason for this?

I am writing the code in Google Cloud Functions.This is the log when I run this function:

 {
 insertId: "-ft02akcfpq"  
 logName: "projects/test-66600/logs/cloudaudit.googleapis.com%2Factivity"  
 operation: {…}  
 protoPayload: {…}  
 receiveTimestamp: "2018-06-15T09:36:13.760751077Z"  
 resource: {…}  
 severity: "NOTICE"  
 timestamp: "2018-06-15T09:36:13.436Z"  
}

{
 insertId: "000000-ab6c5ad2-3371-429a-bea2-87f8f7e36bcf"  
 labels: {…}  
 logName: "projects/test-66600/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
 receiveTimestamp: "2018-06-15T09:36:17.865654673Z"  
 resource: {…}  
 severity: "ERROR"  
 textPayload: "Warning, estimating Firebase Config based on GCLOUD_PROJECT. Intializing firebase-admin may fail"  
 timestamp: "2018-06-15T09:36:09.434Z"  
}
1

1 Answers

0
votes

I have tried the same code and it works for me. However, I have noticed that there was a delay before the entities appeared in Datastore. In order to update and overwrite existing entities, use .upsert(task) instead of .save(task) (link to GCP documentation). You can also use .insert(task) instead of .save(task) to store new entities.

Also check that the project id is correct and that you are inspecting the entities for the right kind.