任何人都可以告诉我,以下错误消息试图告诉我什么?
严重错误:未捕获的异常'InvalidArgumentException'与 消息“提供的阵列中不存在键写入”。在 /vendor/google/cloud/Core/src/ArrayTrait.php:38
Stack trace:
#0 /vendor/google/cloud/Firestore/src/Connection/Grpc.php(127): Google\Cloud\Firestore\Connection\Grpc->pluck('writes', Array)
#1 /vendor/google/cloud/Firestore/src/WriteBatch.php(381): Google\Cloud\Firestore\Connection\Grpc->commit(Array)
#2 import.php(45): Google\Cloud\Firestore\WriteBatch->commit()
#3 {main} thrown in /vendor/google/cloud/Core/src/ArrayTrait.php on line 38
我的代码如下:
$batch = $project->db->batch();
foreach($memberList as $member){
$addedDocRef = $collection->newDocument();
$data["id"] = $addedDocRef->id();
$data["creation"] = $this->generateCreation();
$data["publication"] = $this->generatePublication();
$batch->create($addedDocRef, $data);
}
$batch->commit();
答案 0 :(得分:2)
亚历山大的答案在特定情况下是正确的。
更通用的解决方案是使用直接在Firestore中提供的功能,该功能可检查是否有任何东西排队入批处理。
if (!$batch->isEmpty()) {
$batch->commit();
}
来源: https://github.com/googleapis/google-cloud-php-firestore/blob/master/src/WriteBatch.php#L483
答案 1 :(得分:1)
它告诉您您正在运行提交,但批处理不包含任何操作。可能在$ memberList为空时出现此错误。一种防止错误的简单方法是:
if (! empty($memberList)) {
$batch->commit();
}
此外,您确定$ batch-> create()存在吗?您应该使用set()方法。这是最新的Firestore文档:
$batch = $db->batch();
# Set the data for NYC
$nycRef = $db->collection('cities')->document('NYC');
$batch->set($nycRef, [
'name' => 'New York City'
]);
# Update the population for SF
$sfRef = $db->collection('cities')->document('SF');
$batch->update($sfRef, [
['path' => 'population', 'value' => 1000000]
]);
# Delete LA
$laRef = $db->collection('cities')->document('LA');
$batch->delete($laRef);
# Commit the batch
$batch->commit();