Back

I can't run functions locally

  • 0
  • Self Hosted
  • Functions
WhMonkey
28 Aug, 2024, 19:26

@Red I’m no expert but it seems to be an issue with your code

TL;DR
Developers are troubleshooting issues with running functions locally. The problem seems to be a typo in the code where 'error' is misspelled as 'erorr'. The corrected code snippet has been provided in the thread.
WhMonkey
28 Aug, 2024, 19:26

Is it the same code that you tested on cloud?

Red
28 Aug, 2024, 19:28

Same code on cloud just imported to local

Red
28 Aug, 2024, 19:28

This is on cloud

WhMonkey
28 Aug, 2024, 19:37

Can you share your code please

Red
28 Aug, 2024, 19:41
WhMonkey
28 Aug, 2024, 19:41

Please copy paste it here

WhMonkey
28 Aug, 2024, 19:42

I cant do anything with a screenshot

Red
28 Aug, 2024, 19:42

` import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:backoffice_shared/backoffice_shared.dart'; import 'package:dart_appwrite/dart_appwrite.dart'; import 'package:starter_template/utils/update_compensations.dart';

Future<dynamic> main(final context) async { final List payslipsData = json.decode(context.req.body)["payslips"]; final List employeeData = json.decode(context.req.body)["employees"]; final List compensationData = json.decode(context.req.body)["compensations"];

final List<PayslipModel> payslips = payslipsData.map((payslip) { return PayslipModel.fromJson(payslip); }).toList();

final List<EmployeeModel> employees = employeeData.map((employee) { return EmployeeModel.fromJson(employee); }).toList();

final List<CompensationModel> compensations = compensationData.map((employee) { return CompensationModel.fromJson(employee); }).toList();

final client = Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID']) .setKey(Platform.environment['APPWRITE_API_KEY']);

final Databases databases = Databases(client); final Messaging messaging = Messaging(client);

try { await updateCompensations( context: context, databases: databases, messaging: messaging, payslips: payslips, employees: employees, compensations: compensations, ); } catch (e) { context.error(e); }

return context.res.empty(); }

`

WhMonkey
28 Aug, 2024, 19:45
TypeScript
Future<dynamic> main(final context) async {
  final Map<String, dynamic> body = json.decode(context.req.body);

  final List payslipsData = body["payslips"] ?? [];
  final List employeeData = body["employees"] ?? [];
  final List compensationData = body["compensations"] ?? [];

  final List<PayslipModel> payslips = payslipsData.map((payslip) {
    return PayslipModel.fromJson(payslip);
  }).where((payslip) => payslip != null).toList();

  final List<EmployeeModel> employees = employeeData.map((employee) {
    return EmployeeModel.fromJson(employee);
  }).where((employee) => employee != null).toList();

  final List<CompensationModel> compensations = compensationData.map((employee) {
    return CompensationModel.fromJson(employee);
  }).where((compensation) => compensation != null).toList();

  final projectId = Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'];
  final apiKey = Platform.environment['APPWRITE_API_KEY'];

  if (projectId == null || apiKey == null) {
    throw Exception('Environment variables not set.');
  }

  final client = Client()
      .setEndpoint('https://cloud.appwrite.io/v1')
      .setProject(projectId)
      .setKey(apiKey);

  final Databases databases = Databases(client);
  final Messaging messaging = Messaging(client);

  try {
    await updateCompensations(
      context: context,
      databases: databases,
      messaging: messaging,
      payslips: payslips,
      employees: employees,
      compensations: compensations,
    );
  } catch (e) {
    context.error(e);
  }

  return context.res.empty();
}

Try this with added checks in it.

Red
28 Aug, 2024, 19:46

Okay let me try

Joshi
28 Aug, 2024, 19:51

Are you really sure your code is the same locally?

Joshi
28 Aug, 2024, 19:51

The error you showed us indicates that you made a typo

Joshi
28 Aug, 2024, 19:52

the method "erorr" does not exists but "error"

Joshi
28 Aug, 2024, 19:52

but in the code you showed us it seems to be correct

WhMonkey
28 Aug, 2024, 19:52

if they copy paste the code I gave them

WhMonkey
28 Aug, 2024, 19:52

it should work

Joshi
28 Aug, 2024, 19:54

I'm really confused. He does nowhere uses the method "erorr" in his code

Joshi
28 Aug, 2024, 19:54

I'm not sure if he sent the correct code

WhMonkey
28 Aug, 2024, 19:55

Im not sure either

WhMonkey
28 Aug, 2024, 19:55

@Red is it working?

Red
28 Aug, 2024, 19:58

Sorry I was moving now trying to test

Red
28 Aug, 2024, 19:58

I just imported from cloud no changes whatsoever

Joshi
28 Aug, 2024, 19:59

show us the code for updateCompensation

Joshi
28 Aug, 2024, 20:00

You are passing context there and that code snippet is missing here

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more