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
- Impossible to get USER after createEmail...
Am using provider to deal with functions linked to appwrite. Here is my login. Future<String?> login(String email, String password) async { try { aw...
- Weird permission failure
when creating an account I use following methods: ``` Future<void> register(String email, String password, String username) async { final user = await accoun...
- Flutter Android oAuth is no more working
I currently don't get the oAuth login to work in flutter android. it works on ios and on web. but when try to use it on Android, i get to the point where the ca...