#cloud function response error

1 messages · Page 1 of 1 (latest)

idle turtle
#
^```

code works fine when ran independently on vscode, but I get this error when I deploy and send request from client side

I need an assistance, cause I'm sure am getting something wrong without knowing yet.
#

cloud function response error

sleek flicker
#

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.

idle turtle
sleek flicker
#

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?

idle turtle
#

{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

sleek flicker
#

What is your createExecution looks like?

idle turtle
#
        functionId: 'automate_service_sender', data: jsonEncode(sendData));

    exec.then((response) {
      final decoded = jsonDecode(response.response);

      if (kDebugMode) {
        print("is: ${decoded.toString()}");
      }
    });
sleek flicker
#

Okay this part looks just fine.

#

Can you share how you parse the payload in the server?

idle turtle
#

dataFromClient = jsonDecode(req.payload);

#

And dataFromClient was declared as dynamic

sleek flicker
#

Okay, and what do you see when you're printing the payload

print(req.payload);
idle turtle
#

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

sleek flicker
#

upright?

#

Meaning?

idle turtle
#

When debugging from vscode

idle turtle
sleek flicker
idle turtle
#

Beginning from initialization of appwrite gives an error and when I change function name to main instead of start

sleek flicker
#

You can't change the name

idle turtle
#

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?
sleek flicker
#

Yes

idle turtle
#

Okay

#

Maybe I'll call start from main

sleek flicker
#

I think you should remove the main function from your code.

idle turtle
#

Though before deploying the function I always used start but if am debugging I always replace start with main

sleek flicker
idle turtle
idle turtle
sleek flicker
idle turtle
#

Okay

sleek flicker
#

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}',
  });
idle turtle
idle turtle
idle turtle
sleek flicker
idle turtle
#

I do not even know what new to try anymore

sleek flicker
#

What you mean
What error are you facing right now?

idle turtle
#
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```
idle turtle
sleek flicker
#

Comment what line?

idle turtle
#

{"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,

sleek flicker
#

Can you show me which line casues the error rn?

idle turtle
# sleek flicker Comment what line?
      .setEndpoint(req.variables["http://localhost/v1"])
      .setProject(req.variables["express_pay_app"])
      .setKey(req.variables['automate_service_sender'])
      .setSelfSigned(status: true);```
sleek flicker
#

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

idle turtle
sleek flicker
#

Let's start over
Right now, what are you trying to do?

idle turtle
sleek flicker
#

Good

#

So what doesn't work?

idle turtle
sleek flicker
#

Ohh
Can you share the client side code?

idle turtle
#
  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()}");
      }
    });
sleek flicker
#

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()}");
      }
    });

idle turtle
sleek flicker
#

Mmm so the error is before?

sleek flicker
idle turtle
sleek flicker
#

It's before decoding the response from the function

idle turtle
#

none before that

#

though I jus added one

sleek flicker
#

Try to also add the stacktrace so it would be easy to spot the problem

try {
  ...
} catch(e, stacktrace) {
    print(e);
    print(stacktrace);
}
idle turtle
# sleek flicker Try to also add the stacktrace so it would be easy to spot the problem ```js try...
[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

sleek flicker
#

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()}");
      }
    });
idle turtle
#

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

sleek flicker
#

It mean't the error is from trying to parse it

idle turtle
#

Apologies for the inconveniences caused

sleek flicker
#

A think in the client side

sleek flicker
#

What is the response contains?

idle turtle
sleek flicker
#

Exactly

idle turtle
#
  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

sleek flicker
#

Yes

idle turtle
#

the above is code response

sleek flicker
#

This is wierd

#

Can you check the execution response and share it from your Appwrite console

idle turtle
#

No response recorded
Under response

Under errors

sleek flicker
#

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
```?
idle turtle
sleek flicker
idle turtle
sleek flicker
#

What OS are you using?

sleek flicker
idle turtle
#

it was making the user app say, there was an error, since I knew what I wanted as response

sleek flicker
idle turtle
sleek flicker
#

But, first what is the OS you're using? so we can enter the right address in the function

sleek flicker
#

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

idle turtle
#

No
eth0

sleek flicker
#

So maybe you have Wireless connection

#

Search for any inet

idle turtle
#

Okay

idle turtle
#

just some docker details for connection

#

Like docker0

#

But local is 127.0.0.1

sleek flicker
#

Do you get any ip running

ip addr | grep inet
idle turtle
#

inet 127.0.0.1/8 scope host lo

And others

#

http://localhost/v1

also opens console in terminal

sleek flicker
#

Is this computer isn't connected to the internet?

idle turtle
#

It does

#

am also using it for communication on discord

sleek flicker
#

Then you must have another ip you set/or got from the router.

idle turtle
#

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

sleek flicker
#

All of them?

#

Can you share all of your ifconfig response?

idle turtle
#

Discord said too long

sleek flicker
#

Send as text file

idle turtle
#

Ok, through?

sleek flicker
#

Here

idle turtle
#

On your DM

sleek flicker
#

Good

idle turtle
#

seems it's dart

sleek flicker
#

You don't see Appwrite?

idle turtle
#

If I declare a function
run() {}
and I call it
run();

compiler will show error on function call, that function is already declared

idle turtle
sleek flicker
idle turtle
sleek flicker
#

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.

sleek flicker
#

Good

idle turtle
#

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'])

idle turtle
#

no variable like that exist in cloud function, or what do you think was the issue, so I don't face that next time

sleek flicker
#

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

idle turtle
#

I didn't get you clearly @sleek flicker

#

thanks for standing by me, I'm super grateful

this bug was looking like something else

sleek flicker
#

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.

idle turtle
sleek flicker
#

Probably as you didn't had that value

idle turtle
#

I appreciate you man, I didn't even think the error was from there

@sleek flicker

sleek flicker
idle turtle
#

currently used

172.19.0.1 and it worked

sleek flicker
#

Yes it just local address

idle turtle