I’m working on an embedded custom react application within DHIS2 - I have gone through the documentation from here - How to use the Data Query Playground | DHIS2 Developer Portal - and have been able to get a new Instance created in the system with no issues. My major issue is relating to the updates of trackedEntityInstances.
I have the following sample JSON - which will import via the import app:
{
"trackedEntityInstances": [ {
"orgUnit": "VgrqnQEtyOP",
"trackedEntityInstance": "HuZ5Kc4GSUe",
"trackedEntityType": "W9FNXXgGbm7",
"attributes": [{
"attribute": "jv1fu2pDYKE",
"value": "01"
}, {
"attribute": "zh91RcBXyEf",
"value": "2004-01-01"
}, {
"attribute": "esA6f27JSQM",
"value": "55"
}, {
"attribute": "ggdR2bH42l3",
"value": "1"
}, {
"attribute": "Zb9icCay6Ka",
"value": "0"
}
]
}
]
}
When attempting to perform the update using this code below
const handleFormSubmit = async (event) => {
event.preventDefault();
const url = 'https://<URL>/api/trackedEntityInstances'; //
const jsonData = {
trackedEntityInstances: [
{
orgUnit: 'VgrqnQEtyOP',
trackedEntityInstance: 'HuZ5Kc4GSUe',
trackedEntityType: 'W9FNXXgGbm7',
attributes: [
{ attribute: 'jv1fu2pDYKE', value: '01' },
{ attribute: 'zh91RcBXyEf', value: '2004-01-01' },
{ attribute: 'esA6f27JSQM', value: '55' },
{ attribute: 'ggdR2bH42l3', value: '1' },
{ attribute: 'Zb9icCay6Ka', value: '0' },
],
},
],
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(jsonData),
});
if (response.ok) {
const data = await response.json();
setResponseMessage('Data uploaded successfully!');
console.log(data);
} else {
setResponseMessage('Failed to upload data');
console.error('Error:', response.statusText);
}
} catch (error) {
setResponseMessage('An error occurred while uploading data');
console.error('Error:', error);
}
};
going via Post I am getting a "Unexpected token ‘<’, "<!DOCTYPE “… is not valid JSON” error.
Alternately I have attempted to just do a dummy update with the same data using the UseDataMutation methods in @dhis2/app-runtime. As shown below:
{
const [responseMessage, setResponseMessage] = useState('');
const mutation = {
resource: 'trackedEntityInstances',
type: 'update',
data: {
trackedEntityInstances: [
{
orgUnit: 'VgrqnQEtyOP',
trackedEntityInstance: 'HuZ5Kc4GSUe',
trackedEntityType: 'W9FNXXgGbm7',
attributes: [
{ attribute: 'jv1fu2pDYKE', value: '01' },
{ attribute: 'zh91RcBXyEf', value: '2004-01-01' },
{ attribute: 'esA6f27JSQM', value: '55' },
{ attribute: 'ggdR2bH42l3', value: '1' },
{ attribute: 'Zb9icCay6Ka', value: '0' },
],
},
],
},
};
const [mutate, { error, loading }] = useDataMutation(mutation);
const handleFormSubmit = async (event) => {
event.preventDefault();
try {
const response = await mutate();
if (!response || (error && error.length > 0)) {
setResponseMessage('Failed to upload data');
console.error('Error:', error);
} else {
setResponseMessage('Data uploaded successfully!');
console.log(response);
}
} catch (error) {
setResponseMessage('An error occurred while uploading data');
console.error('Error:', error);
}
Going this route - I’m getting an Method not allowed
Can anyone advise on where I can find simple details of an update of an TrackedEntityInstance via the UseDataMutation - even an example via GitHub that I can compare where I’m going wrong.
If I update the type: ‘update’, to type: ‘create’, it runs without issue.