如何在php中初始化Cloud Firestore?

问题描述 投票:0回答:1

我按照这些教程使用PHP中的Firestore:https://github.com/googleapis/google-cloud-php-firestore https://firebase.google.com/docs/firestore/quickstart

但是当我加载页面时,会显示以下消息:

Google \ ApiCore \ ValidationException:呈现'projects / {project =} / databases / {database =}'时出错:期望绑定'project'以匹配段'{project = *}',而不是null null提供绑定:Array([project ] => [数据库] =>(默认))在第238行的C:\ wamp64 \ www \ php \ vendor \ google \ gax \ src \ ResourceTemplate \ RelativeResourceTemplate.php'

这是我的代码:

composer.json:

 {
    "require": {
        "google/cloud-firestore": "^1.4",
        "grpc/grpc": "v1.19.0"
    }
}

index.php(例子的副本):

require ('vendor/autoload.php');
use Google\Cloud\Firestore\FirestoreClient;

/**
 * Initialize Cloud Firestore with default project ID.
 * ```
 * initialize();
 * ```
 */
    // Create the Cloud Firestore client
    $db = new FirestoreClient();
    printf('Created Cloud Firestore client with default project ID.' . 
PHP_EOL);



$docRef = $db->collection('users')->document('lovelace');
$docRef->set([
    'first' => 'Ada',
    'last' => 'Lovelace',
    'born' => 1815
]);
php firebase google-cloud-firestore grpc google-api-php-client
1个回答
3
投票

看起来您没有设置默认项目ID。

尝试将GCLOUD_PROJECT环境变量设置为项目ID。

set GCLOUD_PROJECT=PROJECT_ID

您也可以尝试设置项目ID,如下所示:

$db = new FirestoreClient([
        'projectId' => 'my-project-id',
    ]);
© www.soinside.com 2019 - 2024. All rights reserved.