php - Firestore : Key writes does not exist in the provided array

标签 php google-cloud-firestore gcloud

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

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Key writes does not exist in the provided array.' in /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();

最佳答案

它告诉您您正在运行提交,但批处理不包含任何操作。可能当您的 $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();

关于php - Firestore : Key writes does not exist in the provided array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51569285/

相关文章:

来自 php 的 .Net Web 服务调用

PHP header 不适用于 Access-Control-Allow-Origin

在 FreeBSD 中从 PHP 5.5 升级后出现 PHP 5.6 错误 "Unable to connect to ssl://"

java - 无法从 firestore 获取数据

firebase - Firestore Timestamp.toDate() 以 UTC 返回日期

go - gcloud 函数部署 go 运行时错误 "undefined: unsafe.Slice; Error ID: 2f5e35a0"

php - Ajax 上传插件抛出 jQuery.handleError not found

ios - 错误 : Index out of range in segment control

bash - -bash : gcloud: command not found on Mac

kubernetes - 为什么我在 alpha 集群创建期间收到 "(gcloud.alpha.container.clusters.create) ResponseError: code=404, message=Method not found."?