#cloud function response error
1 messages · Page 1 of 1 (latest)
This usually happened when the payload data is not a valid JSON.
Can you print the payload data and make sure it's a valid JSON with all the double quotes, trailing comma, etc.
on the cloud function or Flutter code?
besides I appreciate you for reaching out @sleek flicker
can't take that for granted
In your cloud function code.
Actually where's the error coming from?
The App?
Can you share the code of how you're running the function?
How do I make it valid then?
{numberToProcess: 62626262666 , userMail: @mail.com , userID: 649f4aa7ab7ysy4b89ba0bd , totalServiceCharge: 200 , serciceTypeAndService: 9MOBILE CG , serviceQuantity: 500MB , walletBalanceDocumentID: 12222222222, networkID: 3 , planID: 329 , serviceName: Mobile Data, customerAmount: 200 , distributorName: none, meterTypeWithID: none, cableNameWithID: none, cablePlanWithID: none}
what was printed on Flutter before sending
What is your createExecution looks like?
functionId: 'automate_service_sender', data: jsonEncode(sendData));
exec.then((response) {
final decoded = jsonDecode(response.response);
if (kDebugMode) {
print("is: ${decoded.toString()}");
}
});
Okay this part looks just fine.
Can you share how you parse the payload in the server?
dataFromClient = jsonDecode(req.payload);
And dataFromClient was declared as dynamic
Okay, and what do you see when you're printing the payload
print(req.payload);
If I try to print that dart will say it does not understand the Appwrite variables and all of that
So I do comment them usually
When debugging from vscode
Appwrite I meant
@sleek flicker
It was a voice to text, was walking on the road
Comment what?
req.payload should be string
Can you print it, or you getting error trying to do so?
Beginning from initialization of appwrite gives an error and when I change function name to main instead of start
You can't change the name
Make sure the function file structure it's like in the starter
https://github.com/appwrite/functions-starter/blob/main/dart-2.17/lib/main.dart
Okay
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned(status: true);
}```
Isn't the above where initialization starts?
Yes
I think you should remove the main function from your code.
Though before deploying the function I always used start but if am debugging I always replace start with main
Let it all run inside the start method
Gotcha, makes sense.
But how do I test it while debugging because dart will always say it does not recognise the presence of main function in the code
?
For that
You can first upvote this issue: https://github.com/appwrite/appwrite/issues/5425
Second you can create another file in your project for now, name it local-server.dart I'll write the code in a minute.
Okay
local-server.dart
import './lib/main.dart' as user_code;
void main() async {
await user_code.start(Request(), Response());
}
class Request {
final Map<String, dynamic> variables;
final Map<String, dynamic> headers;
final String payload;
Request({
this.variables = const {},
this.headers = const {},
this.payload = '',
});
}
class Response {
int _status = 200;
dynamic _text;
int get status => _status;
dynamic get body => _text;
Response send(String? text, {int status = 200}) {
print(text);
return this;
}
Response json(Map<String, dynamic> json, {int status = 200}) {
print(json);
return this;
}
}
Now you can keep your main file intact and just run the local-server
dart local-server.dart
Put the local-server.dart file in the main function directory.
Btw, you can add in the payload property inside the Request class any possible payload information, like:
Request({
this.variables = const {},
this.headers = const {},
this.payload = '{"username":"john", "age":22}',
});
woow, I appreciate this
Was offline for some hours
@sleek flicker
@sleek flicker
I appreciate you
but sincerely I still don't understand what you wrote
tried allI could, no solution yet
That file is just for test in your local machine
okay, I made a change and discovered it is my appwrite initialization
I do not even know what new to try anymore
What you mean
What error are you facing right now?
NoSuchMethodError: Class 'String' has no instance getter 'variables'.
Receiver: "{\"numberToProcess\":\"00123456789\",\"userMail\":\"dammy\",\"userID\":\"52s651652s17js8\",\"totalServiceCharge\":100,\"serciceTypeAndService\":\"MTN SME\",\"serviceQuantity\":\"10GB\",\"walletBalanceDocumentID\":\"yiwstw\",\"networkID\":\"1\",\"planID\":\"329\",\"serviceName\":\"Mobile Data\",\"customerAmount\":100,\"distributorName\":\"none\",\"meterTypeWithID\":\"none\",\"cableNameWithID\":\"none\",\"cablePlanWithID\":\"none\"}"
Tried calling: variables```
this is a map, that was enoded and when decoded and printed, I get correct answer, but when I uncomment the appwrite call, I get the above error
Comment what line?
{"numberToProcess":"00123456789","userMail":"dammy","userID":"52s651652s17js8","totalServiceCharge":100,"serciceTypeAndService":"MTN SME","serviceQuantity":"10GB","walletBalanceDocumentID":"yiwstw","networkID":"1","planID":"329","serviceName":"Mobile Data","customerAmount":100,"distributorName":"none","meterTypeWithID":"none","cableNameWithID":"none","cablePlanWithID":"none"}
the above result is gotten if I print before and after encode and decode,
Can you show me which line casues the error rn?
.setEndpoint(req.variables["http://localhost/v1"])
.setProject(req.variables["express_pay_app"])
.setKey(req.variables['automate_service_sender'])
.setSelfSigned(status: true);```
And you get the error when trying to run the local-server.dart?
Also change this code to contain the values
client
.setEndpoint("http://localhost/v1")
.setProject("express_pay_app")
.setKey("API_KEY")
.setSelfSigned(status: true);
Unless http://localhost/v1 is a key inside your variables
the above
you meant?
Let's start over
Right now, what are you trying to do?
Send a request to cloud function from client after it is encoded and cloud function should decode it and work on it
I get an error at client side by try catch in cloud function
Ohh
Can you share the client side code?
sure
dynamic data = {
/// generic
"numberToProcess": "${phoneNumberController.text} ",
/// for manual and auto --- refund and database
"userMail": "${homeController.userEmail} ",
"userID": "${homeController.userID} ",
"totalServiceCharge": "${networkParameters[2].toString()} ",
"serciceTypeAndService": "${networkParameters[0].toString()} ",
"serviceQuantity": "${networkParameters[3].toString()} ",
/// user phone number was used as wallet balace document
"walletBalanceDocumentID":
homeController.walletBalanceDocumentID.toString(),
/// for auto
"networkID": "${networkParameters[5].toString()} ",
"planID": "${networkParameters[1].toString()} ",
"serviceName": "Mobile Data",
"customerAmount": "${networkParameters[2].toString()} ",
///////////////////////////////////////////////////////////
"distributorName": "none",
"meterTypeWithID": "none", "cableNameWithID": "none",
"cablePlanWithID": "none",
};
dynamic exec = appWriteLogicExtender.functions.createExecution(
functionId: 'automate_service_sender', data: jsonEncode(data));
exec.then((response) {
final decoded = jsonDecode(response.response).toString();
if (kDebugMode) {
print("is: ${decoded.toString()}");
}
});
Can you print the response.response value?
exec.then((response) {
print(response.response);
final decoded = jsonDecode(response.response).toString();
if (kDebugMode) {
print("is: ${decoded.toString()}");
}
});
Mmm so the error is before?
All this code is inside try/catch?
you meant?
It's before decoding the response from the function
Try to also add the stacktrace so it would be easy to spot the problem
try {
...
} catch(e, stacktrace) {
print(e);
print(stacktrace);
}
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:501:7)
#2 _parseJson (dart:convert-patch/convert_patch.dart:36:10)
#3 JsonDecoder.convert (dart:convert/json.dart:610:36)
#4 JsonCodec.decode (dart:convert/json.dart:216:41)
#5 jsonDecode (dart:convert/json.dart:155:10)
#6 MobiledataController.automatedMobileDataSender.<anonymous closure>
mobiledata_controller.dart:298
<asynchronous suspension>
same error
So it is in the jsonDecode function
What happened when you checking the response and returning before.
exec.then((response) {
print(response.response);
return;
final decoded = jsonDecode(response.response).toString();
if (kDebugMode) {
print("is: ${decoded.toString()}");
}
});
I didn't get your message
What am saying is that while I was debugging I decided to encode and decode a map from the cloud function code to see if it's the cloud function code that's having error, but works
It mean't the error is from trying to parse it
Typing?
A back end or client side error?
Apologies for the inconveniences caused
A think in the client side
When you're running it that way
What is the response contains?
From server to client?
Exactly
exec.then((response) {
print(response.response);
return;
final decoded = jsonDecode(response.response).toString();
if (kDebugMode) {
print("is: ${decoded.toString()}");
}
});
this is the code for it
To get response
Yes
the above is code response
This is wierd
Can you check the execution response and share it from your Appwrite console
Okay
No response recorded
Under response
Under errors
Okay, so your client side is okay you just need to add catch
exec.then((response) {
print(response.response);
return;
final decoded = jsonDecode(response.response).toString();
if (kDebugMode) {
print("is: ${decoded.toString()}");
}
}).catch((e){
});
As the error is legit.
In your function code what do you have here?
client
.setEndpoint
```?
I added catch to client, same error
Of course
It's good to have the error as there's an error.
You'll need to notify the end-user
client
.setEndpoint("http://localhost/v1")
.setProject("express_pay_app")
.setKey("API_KEY")
.setSelfSigned(status: true);
This is good execpt one part that was my mistake
What OS are you using?
Am the end user, am testing
Yep, then show some alert or adapt the view
like how?
it was making the user app say, there was an error, since I knew what I wanted as response
- Alert
- Toast
- Whole error screen
Alert dialog
But, first what is the OS you're using? so we can enter the right address in the function
Linux
Then run ifconfig
And get your local IP
Then enter this IP in the browser to make sure you see Appwrite
Then put this address here
For example
client
.setEndpoint("http://192.168.235.255/v1")
.setProject("express_pay_app")
.setKey("API_KEY")
.setSelfSigned(status: true);
Then redeploy the function and check
No
eth0
Okay
None
just some docker details for connection
Like docker0
But local is 127.0.0.1
Do you get any ip running
ip addr | grep inet
inet 127.0.0.1/8 scope host lo
And others
http://localhost/v1
also opens console in terminal
Is this computer isn't connected to the internet?
Then you must have another ip you set/or got from the router.
What happened when you go this urls in your browser
http://10.0.0.138
http://192.168.0.1
http://192.168.1.1
I have another cloud function that once gave that error, but not doing that currently, with expected response been gotten, I followed the pattern of that one, same error
Delaying to load
Send as text file
Ok, through?
On your DM
seems it's dart
You don't see Appwrite?
If I declare a function
run() {}
and I call it
run();
compiler will show error on function call, that function is already declared
No
Which address show's Appwrite?
http://172.19.0.1
http://172.20.0.1
http://172.18.0.1
http://172.17.0.1
http://192.168.43.140
Show appwrite login
Sweet
Put it inside your function like so
client
.setEndpoint("http://192.168.43.140/v1")
.setProject("express_pay_app")
.setKey("API_KEY")
.setSelfSigned(status: true);
Then redeploy and test.
App shows appwrite
Good
Try this
Okay
Worked
Wooooooooooooooooooow
am surprised 🙀😯
wooow, you're a genuine helper
a genius man
@sleek flicker
got response for first time in one week 🙁
This error ate my time but I will catch up
But why was other cloud function working with different address
.setKey(req.variables['automate_service_sender'])
this probably was the cause
no variable like that exist in cloud function, or what do you think was the issue, so I don't face that next time
The problem now was that Appwrite isolated function can't and won't be able to parse localhost as server localhost.
For this instance make sure you have - or you can create - a variable named automate_service_sender in the cloud
I didn't get you clearly @sleek flicker
thanks for standing by me, I'm super grateful
this bug was looking like something else
For you to able to get variables from req.variables map
You'll need to add it in your Appwrite console.
You can do so when you go to your function -> settings -> varaibles section
You can see in the example I've added the automate_service_sender now it will be accessible through the req.variables['automate_service_sender'] inside the function.
Okay, this was the cause of the error right?
Probably as you didn't had that value
you're right
I appreciate you man, I didn't even think the error was from there
@sleek flicker

seems I may always chage the ip when my system restart right?
currently used
172.19.0.1 and it worked
Yes it just local address
okay, thanks