I am currently facing an issue after upgrading to 1.4.3 regarding the responseBody
when returning JSON from the function.
So my Node.js function has the following lines:
log(JSON.stringify(myContainers));
return res.json(JSON.stringify(myContainers));
The appwrite log output show the following (as desired) value:
[{"_id":"649808696bca89048c8f0653","name":"Rombergstraße 118","label":"Weihnachtsdeko","uuid":"6a17cb1b-4215-4538-a2ae-0ef8a0af7fc8","location":"Unbekannt","description":"","tags":["test"],"owners":[{"username":"testuser@example.com","name":"Test User","userId":"649807cb6488dc8ff3c5"}],"readUsers":[],"rfid":""},{"_id":"649808696bca89048c8f0654","name":"Bethmannstraße 115","label":"Behälter 2","uuid":"e97b24c3-df68-45ef-a7dd-c830205cfae0","location":"Unbekannt","tags":["test"],"owners":[{"username":"testuser@example.com","name":"Test User","userId":"649807cb6488dc8ff3c5"}],"readUsers":[]}]
But the responseBody
of the request, when using Postman returns:
"responseBody": "\"[{\\\"_id\\\":\\\"649808696bca89048c8f0653\\\",\\\"name\\\":\\\"Rombergstraße 118\\\",\\\"label\\\":\\\"Weihnachtsdeko\\\",\\\"uuid\\\":\\\"6a17cb1b-4215-4538-a2ae-0ef8a0af7fc8\\\",\\\"location\\\":\\\"Unbekannt\\\",\\\"description\\\":\\\"\\\",\\\"tags\\\":[\\\"test\\\"],\\\"owners\\\":[{\\\"username\\\":\\\"testuser@atic.ar\\\",\\\"name\\\":\\\"Test User\\\",\\\"userId\\\":\\\"649807cb6488dc8ff3c5\\\"}],\\\"readUsers\\\":[],\\\"rfid\\\":\\\"\\\"},{\\\"_id\\\":\\\"649808696bca89048c8f0654\\\",\\\"name\\\":\\\"Bethmannstraße 115\\\",\\\"label\\\":\\\"Behälter 2\\\",\\\"uuid\\\":\\\"e97b24c3-df68-45ef-a7dd-c830205cfae0\\\",\\\"location\\\":\\\"Unbekannt\\\",\\\"tags\\\":[\\\"test\\\"],\\\"owners\\\":[{\\\"username\\\":\\\"testuser@atic.ar\\\",\\\"name\\\":\\\"Test User\\\",\\\"userId\\\":\\\"649807cb6488dc8ff3c5\\\"}],\\\"readUsers\\\":[]}]\"",
The problem is: the response is surrounded with additional "
which are escaped (\"
).
The issue is, in my Flutter code the jsonDecode
function interprets the value of the reponseBody
as a string, instead of an dynamic object.
The value I have copied is directly from Postman to have a more RAW response and out rule package issues from Flutter.
So I created a minimum example:
TypeScript file
type Context = {
req: any;
res: any;
log: (msg: any) => void;
error: (msg: any) => void;
};
class TestModel {
constructor(firstname: string, lastname: string){
this.firstname = firstname;
this.lastname = lastname;
}
firstname: string;
lastname: string;
};
export default async ({ req, res, log, error }: Context) => {
let testObject = new TestModel("John", "Doe");
return res.json(JSON.stringify(testObject));
};
The responseBody
is:
{
...
"responseBody": "\"{\\\"firstname\\\":\\\"John\\\",\\\"lastname\\\":\\\"Doe\\\"}\"",
...
}
Alright I think I understood the mistake. The res.json
function changed in such a way, that the json.stringify()
is done implicitly. So changing ...
res.json(JSON.stringify(testObject));
to ...
res.json(testObject);
should do the trick. 🙂
You shouldn't have even been doing res.json(JSON.stringify(testObject))
in the previous versions 👀
[SOLVED] responseBody does not contain a valid JSON in appwrite function
Recommended threads
- Project in AppWrite Cloud doesn't allow ...
I have a collection where the data can't be opened. When I check the functions, there are three instances of a function still running that can't be deleted. The...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...
- Function in Node.JS to monitor events ar...
Hello everyone. I'm creating my first Node.JS function, but I don't have much experience with node and javascript. I'm trying to create a function, that monito...