#[SOLVED] The function always gets timed out

146 messages · Page 1 of 1 (latest)

winged hollow
#

I tried all the things, but the functions is always getting timed out, after about 15-17 seconds, heres the code

import 'dart:async';

import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {
  final client = Client();

  final database = Databases(client);
  final document = await database.createDocument(
    databaseId: '6489ef1fb252d4ac177d',
    collectionId: '6489ef320a5a9b83cd78',
    documentId: 'unique()',
    data: {...},
  );

  res.json({document.toMap()});
}
stray dew
#

Be sure to set your Client with you server data

Client client = Client()
    .setEndpoint('https://cloud.appwrite.io/v1') 
    .setProject('[PROJECT_ID]')              
    .setKey('919c2db5d4...a2a3346ad2');     

You can read more about it here https://appwrite.io/docs/getting-started-for-server.

winged hollow
#

But if im running it in functions, then also?

#

If so, do i generate a key for the same?

winged hollow
#

Its actually a appwrite functions code that ive pushed

#

this one didnt had any, so got confused

stray dew
#

Appwrite function are isolated.
So if you want to connect to Appwrite then you'll to set the endpoint

winged hollow
#

Oook, my bad i didnt checked the below part

stray dew
#

No worries

winged hollow
#

Its still getting timed out

#

I added project, endpoint, key and self signed

#

also the key has all the required permissions

stray dew
#

Have you set the endpoint with localhost?

winged hollow
#

yes

#

It also has /v1 with it

#

and also tried without /v1

stray dew
#

You do need to v1

#

But localhost can't be used inside a function

#

What you should do locally is to use your host IP address

#

What os you using

winged hollow
#

macOS

#

On apple silicon

winged hollow
stray dew
#

Run

ifconfig
stray dew
winged hollow
stray dew
stray dew
winged hollow
#

Really sorry but going to sleep so will check it tomorrow

#

Really sorry for it

stray dew
#

No problem

#

👍

winged hollow
#

I also tried by it

#

I actually hosted the server using ngrok and thereby deployed it and also added the endpoint of ngrok

#

I tried bot with /v1 and without it

stray dew
#

Can you share the init code?

winged hollow
#

Heres it

#
import 'dart:async';

import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {
  final client = Client()
      .setEndpoint('https://....ngrok-free.app')
      .setProject('64845d3615cdde71420c')
      .setKey('...')
      .setSelfSigned(status: true);

  final database = Databases(client);
  final document = await database.createDocument(
    databaseId: '6489ef1fb252d4ac177d',
    collectionId: '6489ef320a5a9b83cd78',
    documentId: 'unique()',
    data: {...},
  );

  res.json({document.toMap()});
}
stray dew
#

Okay,

#

First we need to add /v1
And to be sure everything is in order go to this url
https://....ngrok-free.app/versions

If you see the JSON, then you know you good, so you can set it like this

Future<void> start(final req, final res) async {
  final client = Client()
      .setEndpoint('https://....ngrok-free.app/v1')
winged hollow
stray dew
#

Okay.

winged hollow
#
{
"server": "1.3.7",
"client-web": "11.0.0",
"client-flutter": "9.0.0",
"client-apple": "2.0.0",
"client-android": "2.0.0",
"client-graphql": "October 2021",
"client-rest": "",
"console-web": "0.1.0",
"console-cli": "2.0.2",
"server-nodejs": "9.0.0",
"server-deno": "7.0.0",
"server-php": "8.0.0",
"server-python": "2.0.0",
"server-ruby": "8.0.0",
"server-dart": "8.0.0",
"server-kotlin": "2.0.0",
"server-swift": "2.0.0",
"server-graphql": "October 2021",
"server-rest": ""
}
#

Heres the output of the versions endpoint

stray dew
#

So when you running it like this
You still get timeout?

winged hollow
#

if so then yes

stray dew
#

Yes

#

Okay, try this

import 'dart:async';

import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {
  try {
      final client = Client()
        .setEndpoint('https://....ngrok-free.app/v1')
        .setProject('64845d3615cdde71420c')
        .setKey('...')
        .setSelfSigned(status: true);

      final database = Databases(client);
      final document = await database.createDocument(
      databaseId: '6489ef1fb252d4ac177d',
      collectionId: '6489ef320a5a9b83cd78',
      documentId: 'unique()',
      data: {...},
    );

    res.json({document.toMap()});
  } catch (e) {
    res.json({error:e});
  }
}

And in your appwrite.json change the timeout value to let's say 300

 "events": [],
 "schedule": "",
 "timeout": 15

Then redeploy and execute the function.

#

Check if you see anything in the function execution data in the console.

winged hollow
#

Also do i set the endpoint without the /v1?

stray dew
#

You're correct
With the /v1

#

The change is that now the function is inside try catch block
So we can see if there's anything else.

winged hollow
#

And ya, i returned error

stray dew
#

What error?

winged hollow
#

the error says Converting object to an encodable object failed: Instance of '_TypeError'

stray dew
#

Can you share this part?

data: {...},

Or any mocking data of what you'll put inside?

winged hollow
stray dew
#

Sure, understood.

#

That means that one of the data properties you've put inside the data was unable to convert.

winged hollow
#

It do has a relationship between two colection

#

Like, im sending a map whihc has a map inside it for relationship

stray dew
#

Meaning you passing something that is not stringify or something,

#

Ohh,

#

Something like this

{
  "data":{
    "children":[
        {...}
     ]
  }
}
#

?

winged hollow
#

ya something like it

stray dew
#

Then, have you put the nasted children inside an array or an hashmap?

winged hollow
#

everything is inside map

#

there isnt any one to many one so

stray dew
#

I think you should put inside an array either case.

#

Give it a try.

winged hollow
#

Okay, lemme try it once

#

Ideally i would be getting data from body of request but as of now, just sending hard coded

#

so it might take a minute

winged hollow
#

No luck, still the error is same: Converting object to an encodable object failed: Instance of '_TypeError'

stray dew
#

Ohh
What version of Appwrite sdk you have in the function pubspec.yml?

winged hollow
#

7.1.0

stray dew
#

Can you change it to 8.0.0?

winged hollow
#

Also I just noticed one thing that it is creating a document in it

#

but without relationship data

#

just the top most document

winged hollow
stray dew
#

Yes, version 7.1.0 is before relationships.

winged hollow
#

oook, so that would be the reason

#

Updated it to 8.0.1

stray dew
winged hollow
#

The deployment failed

stray dew
#

With what error?

winged hollow
#
The current Dart SDK version is 2.17.7.

Because every version of appwrite_function from path depends on dart_appwrite ^8.0.1 which depends on http >=0.13.6 <1.0.1, every version of appwrite_function from path requires http >=0.13.6 <1.0.1.
So, because http >=0.13.6 requires SDK version >=2.19.0 <3.0.0 or ^3.0.0 and open_runtimes_dart depends on appwrite_function from path, version solving failed.
Resolving dependencies...
#

Heres the error

stray dew
#

Yep

#

It's a conflict in the package

#

Add version 8.0.0. But, without the caret ^

winged hollow
#

Should i also upgrade the dart to latest?

stray dew
#

Like so:

dependencies:
  dart_appwrite: 8.0.0
stray dew
winged hollow
#

It did deployed this time

#

but still the resposne is same error

#

Converting object to an encodable object failed: Instance of '_TypeError'

stray dew
#

So, there is something inside the object that is not quite parseable
Can you share a demo version of the data,
Like create fake fields and values but the same is data?

timber plinth
winged hollow
#

Does it requires in json format?

#

Or in a map?

stray dew
#

It required to be able to convert to JSON

timber plinth
winged hollow
#

Yes, it was using from jsona dn to json, so got confused

stray dew
#

You have dart on your machine?

timber plinth
stray dew
#

Ohh
Nice

winged hollow
#

The request is something like this

#
{
        'counter': 2,
        'type': 'ToDo',
        'detail': [
          {
            'name': 'A Todo',
            'description': 'Some description',
            'priority': 2,
          }
        ]
      }
#

I have changed both the key and value but ya, the datatype is the same

#

Also the relationship is one to one

#

Sorry but, as its all under NDA, so I cant share exact details

stray dew
#

That looks right.

#

Try these two things.

#
  1. Send this back instead of the map
    res.json({"document": document});
#
  1. Try to run this locally
import 'dart:convert';

void main() {
  try {
    final str = jsonEncode({'data': {...}});
    print(str);
  } catch (e) {
    print(e);
  }
}

Check if you get your data or you get an error.

winged hollow
#

Now can you please explain on what was the exact error?

stray dew
#

Yep

timber plinth
stray dew
#

When res.json gets an object it will be in charge for converting it and sending it back to the user.
You most provide a key for the value in the JSON you're sending
And when you've used toMap on is own you won't have a key as toMap returns JSON it self
So it will be like you wrote this

    res.json({{
      "id":2
    }});
winged hollow
#

Ohhh, now i see, i added that{}

#

Now i see it

stray dew
#

And there's no need to use toMap as res.json will take care of it.

winged hollow
#

There was no key passed, thanks a lot to both of you, you really helped me a lot

timber plinth
stray dew
winged hollow
timber plinth
stray dew
winged hollow
#

[Solved] The function always gets timed out