Back

function execution error

  • 0
  • Functions
  • Web
  • Cloud
sai pranay
7 Jun, 2023, 13:47

i have no idea why my function is failing

deployed success message:

TypeScript
➜  webcrafts appwrite functions createDeployment --functionId=648077c945bd1641f0a1 --entrypoint='src/index.js' --code="./functions/likeCraftById" --activate=true
Info Ignoring files using configuration from appwrite.json
$id : 648086cf483237eb2bb0
$createdAt : 2023-06-07T13:31:59.380+00:00
$updatedAt : 2023-06-07T13:31:59.380+00:00
resourceId : 648077c945bd1641f0a1
resourceType : functions
entrypoint : src/index.js
size : 4485
buildId :
activate : true
status :
buildStdout :
buildStderr :
buildTime : 0
TL;DR
The user is experiencing an error when running a command to deploy a function in an Appwrite project. They are using the full command approach, which can sometimes be tricky. They are also using the Appwrite CLI to manage and deploy functions. The deployed function is unable to access the index.js file. The user has provided the function code and the command they are using to execute it. They are unsure why their function is failing. Solution: - Use the `Appwrite deploy function` command instead of the full command approach. - Ensure that the deployed function has the necessary permissions to access the `index.js` file. - Check
sai pranay
7 Jun, 2023, 13:47

function code :

TypeScript
const sdk = require("node-appwrite");

module.exports = async function (req, res) {
  const client = new sdk.Client();

  const database = new sdk.Database(client);

  const { craftId, likes } = JSON.parse(req.payload);

  if (
    !req.variables["APPWRITE_FUNCTION_ENDPOINT"] ||
    !req.variables["APPWRITE_FUNCTION_API_KEY"]
  ) {
    console.warn(
      "Environment variables are not set. Function cannot use Appwrite SDK."
    );
  } else {
    client
      .setEndpoint(req.variables["APPWRITE_FUNCTION_ENDPOINT"])
      .setProject(req.variables["APPWRITE_FUNCTION_PROJECT_ID"])
      .setKey(req.variables["APPWRITE_FUNCTION_API_KEY"])
      .setSelfSigned(true);

    let newCraft = {};
    if (craftId) {
      try {
        newCraft = await database.updateDocument(
          req.variables["APPWRITE_FUNCTION_DATABASE_ID"],
          req.variables["APPWRITE_FUNCTION_COLLECTION_ID"],
          craftId,
          {
            likes,
          }
        );
        return res.json({ data: newCraft });
      } catch (e) {
        console.error(e);
      }
    }
  }

  res.json({
    areDevelopersAwesome: true,
  });
};

this is how I am executing

TypeScript
try {
        const functions = new Functions(client);
        await functions.createExecution(
          "FUNCTIoN_ID",
          JSON.stringify({
            craftId: craftid,
            likes: likeCount + 1,
          }),
          true
        );

        setLikeCount(likeCount + 1);

        setLiked(true);
      } catch (error) {
        console.log(error);
      }
sai pranay
7 Jun, 2023, 13:47
Binyamin
7 Jun, 2023, 13:50

What's the error?

sai pranay
7 Jun, 2023, 14:08

Check the image

Binyamin
7 Jun, 2023, 14:11

It doesn't work

sai pranay
7 Jun, 2023, 14:11
sai pranay
7 Jun, 2023, 14:12

Sorry my bad

Binyamin
7 Jun, 2023, 14:13

Okay, that mean the deployed function can't access the index.js file

Binyamin
7 Jun, 2023, 14:13

What I'll suggest is to use Appwrite cli for managing and deploying your functions

Binyamin
7 Jun, 2023, 14:13

Like such

sai pranay
7 Jun, 2023, 14:13

I used cli itself

sai pranay
7 Jun, 2023, 14:14

.

Binyamin
7 Jun, 2023, 14:15

I know, But using the full command approach

TypeScript
appwrite functions createDeployment --functionId=648077c945bd1641f0a1 --entrypoint='src/index.js' --code="./functions/likeCraftById" --activate=true

Can be sometime tricky

sai pranay
7 Jun, 2023, 14:16

Ok so your suggestion is to run

Appwrite deploy function

Instead of full command

Binyamin
7 Jun, 2023, 14:18

Yes, But it should be an Appwrite project before

sai pranay
7 Jun, 2023, 14:19

Yup yup I did that

sai pranay
7 Jun, 2023, 14:20

Thanks i will try that

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