1

Whenever I run the function it will just run until it crashes stating the error is unknown. I feel it may be something to do with the version of the datastore dependency in my package.json file

package.json

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "@google-cloud/datastore": "5.1.0"
  }
} 

index.js

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */

// Requested from front end
var date = "2002-09-13";
var limit = "10";

exports.checkRecords = async (req, res) => {
  // Search for date in Datastore
    const {Datastore} = require('@google-cloud/datastore');
    const datastore = new Datastore({
        projectId: '...',
        keyFilename: '...'
    });

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

    if(entity != null){
      res.status(200).send("contains");
    }
    else{
      res.status(200).send("null");
    }
};

I've resorted to simply seeing if the entity is null because nothing else seems to be working

2 Answers 2

2

I think you're missing the Kind in the get

I created a Dog Kind and added Freddie to it and can:

var date = "2002-09-13";
var limit = "10";

exports.checkRecords = async (req, res) => {
    const { Datastore } = require("@google-cloud/datastore");
    const datastore = new Datastore();
    const taskKey = datastore.key(["Dog", "Freddie"]);
    const [entity] = await datastore.get(taskKey);

    if (entity != null) {
        res.status(200).send("contains");
    }
    else {
        res.status(200).send("null");
    }
};

NB If your Datastore is in the same project, you can use Application Default Credentials to simplify the auth, i.e. const datastore = new Datastore();

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Such a simple fix. Using the default Datastore constructor. I was treating the function like an external one and therefore thought it had to authenticate through the Datastore constructor but that's not required given everything is already in the same project
Another optimization is to pull the require and the constructor out into the global scope. Cloud Functions doesn't create a new instance of your code every invocation and this will reuse.
1

Can you try:

var date = "2002-09-13";
var limit = "10";

exports.checkRecords = async (req, res) => {
  // Search for date in Datastore
    const {Datastore} = require('@google-cloud/datastore');
    const datastore = new Datastore({
        projectId: '...',
        keyFilename: '...'
    });

    // The kind of the entity
    const kind = 'Task';

    // The name/ID of the entity
    const name = 'sampletask1';

    // The Cloud Datastore key for the new entity
    const taskKey = datastore.key([kind, name]);
    const [entity] = await datastore.get(taskKey);

    if(entity != null){
      res.status(200).send("contains");
    }
    else{
      res.status(200).send("null");
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.