I can create normal records using Firestore API with React native.
I’m having trouble creating Array and Map data type records.
The error I get is;
cannot convert firestore v1 value with type unset
JavaScript, PHP and firestore structure is as follows.
JavaScript
axios
.post(
'https://example.com/API/createGroup.php?devices=' +
[{"id":"device1"},{"id":"device2"}] +
'&users=' +
[{"id":"user1"},{"id":"user2"},{"id":"user3"}] +
'&permissions=' +
{"read":true,"write":true} +
'&webToken=' +
WebToken,
)
PHP
<?php
$devices = json_decode($_GET['devices'], true);
$users = json_decode($_GET['users'], true);
$permissions = json_decode($_GET['permissions'], true);
$webToken = $_GET['webToken'];
$curl = curl_init();
$devicesArray = array_map(function($device) {
return [
"mapValue" => [
"fields" => [
"id" => ["stringValue" => $device['id']]
]
]
];
}, $devices);
$usersArray = array_map(function($user) {
return [
"mapValue" => [
"fields" => [
"id" => ["stringValue" => $user['id']]
]
]
];
}, $users);
$firestoreData = [
"fields" => [
"devices" => [
"arrayValue" => [
"values" => $devicesArray
]
],
"users" => [
"arrayValue" => [
"values" => $usersArray
]
],
"permissions" => [
"mapValue" => [
"fields" => [
"read" => ["booleanValue" => $permissions['read']],
"write" => ["booleanValue" => $permissions['write']]
]
]
]
]
];
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://firestore.googleapis.com/v1/projects/exampleProjectName/databases/(default)/documents/groups?key='.$webToken,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($firestoreData),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Any insightful answer will be appreciated. Thank you.