【发布时间】:2019-10-19 06:53:51
【问题描述】:
我在从 Google 数据存储区检索实体时遇到问题。这是我的代码:
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 是另一个实体的字符串 ID,我想将其存储在名为 session_tasks 的属性数组中。
但它并没有那么远。在这行之后:
const [task] = await transaction.get(taskKey);
错误是task 未定义:
ERROR: TypeError: Cannot read property 'session_tasks' of undefined
at pushTaskIdToCurrentSession
从这段代码中可以立即看出什么?
更新:
改用这个:
const task = await transaction.get(taskKey).catch(console.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)
这表明该数组不受支持?
【问题讨论】:
-
你能去掉解构赋值并在
await之后显示(用console.log)task的值吗? -
用完整的错误更新了帖子。
-
“解构赋值”是什么意思?
-
你能试试这个代码
const task = await transaction.get(taskKey);并显示console.log(task)的输出吗? -
我确实尝试过。结果相同。会再去一次...
标签: node.js google-app-engine google-cloud-datastore