skip to Main Content

I used Google.Apis.Firestore.v1 library to create a cloud firestore for my firebase project as follows, but when running my code I am getting an error related to the database naming I guess, and it has a regular expression format as shown in the error below, and how can I fix this issue?

using Google.Apis.Firestore.v1;

private static FirestoreService _firestoreService;
public static void IntializeFirestoreAdmin() {
   GoogleCredential credential = GoogleCredential.GetApplicationDefault();
   if (CloudManager.Credential.IsCreateScopedRequired)
   {
       credential = CloudManager.Credential.CreateScoped(FirestoreService.Scope.CloudPlatform);
            }
       _firestoreService = new FirestoreService(
        new BaseClientService.Initializer()
        {                 
           HttpClientInitializer = credential,
           ApplicationName = CloudManager.ApplicationName

        });          
            

        }
 public static void AddCloudFirestore() {
       IntializeFirestoreAdmin();
       var mydata = new GoogleFirestoreAdminV1Database {
                LocationId = "nam5",
                Type = "FIRESTORE_NATIVE",
                Name = "projects/" + CloudManager.ProjectId + "/databases/(default)",
                
            };
      _firestoreService.Projects.Databases.Create(mydata, "projects/" + CloudManager.ProjectId).Execute();
}

Error :

Unhandled exception. The service firestore has thrown an exception.
HttpStatusCode is BadRequest.
Google.Apis.Requests.RequestError
database_id should be 4-63 characters, and valid characters are /[a-z][0-9]-/ [400]
Errors [
    Message[database_id should be 4-63 characters, and valid characters are /[a-z][0-9]-/] Location[ - ] Reason[badRequest] Domain[global]
]

Google.GoogleApiException: The service firestore has thrown an exception. HttpStatusCode is BadRequest. database_id should be 4-63 characters, and valid characters are /[a-z][0-9]-/
   at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()

2

Answers


  1. Chosen as BEST ANSWER
    var body = new GoogleFirestoreAdminV1Database
    {
        Type = "FIRESTORE_NATIVE",
        LocationId = "nam5"
    };
    string parent = $"projects/{CloudManager.ProjectId}";
    var request = _firestoreService.Projects.Databases.Create(body, parent);
    request.DatabaseId = "(default)";
    var response = request.Execute();
    

    The issue was that I didn't add the databaseId as a query parameter. For more details go to this link https://groups.google.com/g/google-cloud-firestore-discuss/c/EQ-04MnPyLk


  2. If you go through the given method :

    FirestoreService().Projects.Databases.Create it has parent as Parameter and databaseId as a Query parameter there is also a try this method section where you can verify your parameters are correct like for example :

    using Google.Apis.Firestore.v1;
    
    private static FirestoreService _firestoreService;
    public static void IntializeFirestoreAdmin() {
       GoogleCredential credential = GoogleCredential.GetApplicationDefault();
       if (CloudManager.Credential.IsCreateScopedRequired)
       {
           credential = CloudManager.Credential.CreateScoped(FirestoreService.Scope.CloudPlatform);
                }
           _firestoreService = new FirestoreService(
            new BaseClientService.Initializer()
            {                 
               HttpClientInitializer = credential,
               ApplicationName = CloudManager.ApplicationName
    
            });          
                
    
            }
     public static void AddCloudFirestore() {
           IntializeFirestoreAdmin();
           var mydata = new GoogleFirestoreAdminV1Database {
                    LocationId = "nam5",
                    Type = "FIRESTORE_NATIVE",
                    Name = "projects/" + CloudManager.ProjectId + "/databases/my-database",
                    
                };
          _firestoreService.Projects.Databases.Create(mydata, "projects/" + CloudManager.ProjectId).Execute();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search