[英]Can't access Datastore through Google Cloud function using node.js
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 文件中数据存储依赖项的版本有关
package.json包.json
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"@google-cloud/datastore": "5.1.0"
}
}
index.js索引.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我已经求助于简单地查看实体是否为空,因为似乎没有其他工作
I think you're missing the Kind in the get
我认为你错过了get
的那种
I created a Dog
Kind and added Freddie
to it and can:我创建了一个Dog
Kind 并将Freddie
添加到其中,并且可以:
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, ie const datastore = new Datastore();
注意如果你的Datastore在同一个项目中,可以使用Application Default Credentials来简化认证,即const datastore = new Datastore();
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");
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.