Firestore:提供的阵列中不存在键写入

Coa*_*ach 5 php gcloud google-cloud-firestore

谁能告诉我,以下错误消息试图告诉我什么?

致命错误:消息为“键写入”的未捕获异常'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
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

$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();
Run Code Online (Sandbox Code Playgroud)

Ale*_*yuk 4

它告诉您正在运行提交,但批处理不包含任何操作。当您的 $memberList 为空时,可能会出现此错误。防止错误的一个简单方法是:

if (! empty($memberList)) {
    $batch->commit();
}
Run Code Online (Sandbox Code Playgroud)

另外,您确定 $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();
Run Code Online (Sandbox Code Playgroud)