Back

Problem with promise error handling in a cloud function

  • 0
  • Functions
  • Web
Matthias Merk
9 Nov, 2023, 10:21

Hello, I'm currently rewriting my Cloudfunctions to the new syntax. I have one problem with error handling in a promise.

Old:

TypeScript
module.exports = async function (req, res) {
 
// some code

  promise.then(
    function (response) {
      console.log(response);
    },
    function (error) {
      console.log(error);
    },
  );

//some code

};

New:

TypeScript
export default async ({ req, res, log, error }) => {
 
  // some code

  promise.then(
    function (response) {
      log(response);
    },
    function (error) {
      error(error);
    },
  );

//some code
};

In the New version I get the Error " error is declared but its value is never read." for the deconstructured context function. I think this is more like a general JavaScript problem, but can you help me out?

TL;DR
The user is facing an issue with error handling in a cloud function. They are rewriting their code to a new syntax and have encountered a problem. The error message they receive is "error is declared but its value is never read." They would like assistance with this issue. Solution: The error is occurring because the user is redeclaring the `error` variable in the new version. To fix this, they should update the code to use a different variable name for error handling, such as `err`.
ideclon
9 Nov, 2023, 10:27

Are you using TS?

Matthias Merk
9 Nov, 2023, 10:35

No

Mosh Ontong
9 Nov, 2023, 14:43

Please verify that if we have the same configuration

TypeScript
{
  "name": "starter-template",
  "version": "1.0.0",
  "description": "",
  "main": "src/main.js",
  "type": "module",
  "scripts": {
    "format": "prettier --write ."
  },
  "dependencies": {
    "node-appwrite": "^9.0.0"
  },
  "devDependencies": {
    "prettier": "^3.0.0"
  }
}
Mosh Ontong
9 Nov, 2023, 14:44

.prettierrc.json

TypeScript
{
  "trailingComma": "es5",
  "tabWidth": 2,
  "semi": true,
  "singleQuote": true
}
Matthias Merk
10 Nov, 2023, 06:09

Yes, I can confirm that it is the same configuration.

Drake
10 Nov, 2023, 19:20

you're redeclaring the error variable which can cause problems.

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