Back

Bug: Appwrite Environment Variables Breaking Document Creation? Need Help!

  • 0
  • Databases
  • Functions
ZiaChoudhary
17 Oct, 2024, 12:41

Problem Statement: Inconsistent Document Creation with Appwrite Environment Variables in Node.js Function

I'm encountering an issue while creating documents using a Node.js function with Appwrite version 1.6.0. The problem occurs when using environment variables to set the endpoint and project ID, as introduced in Appwrite 1.6.0.

Current Behavior

  • When using hardcoded values for the endpoint and project ID, document creation works as expected.
  • When using environment variables (process.env.APPWRITE_FUNCTION_API_ENDPOINT and process.env.APPWRITE_FUNCTION_PROJECT_ID), document creation fails silently.

Error Logs

TypeScript
documentX created with ID: 6710ffc800129bb4cfaf and data: {"name":"John Doe","age":30,"$id":"6710ffc800129bb4cfaf","$permissions":[],"$createdAt":"2024-10-17T12:15:04.549+00:00","$updatedAt":"2024-10-17T12:15:04.549+00:00","$databaseId":"6709f300000a8e07a263","$collectionId":"6710f9b5000395f9d3f0"}

documentY created with ID: undefined and data: {"total":1,"documents":[{"name":"John Doe","age":30,"$id":"6710ffc800129bb4cfaf","$createdAt":"2024-10-17T12:15:04.549+00:00","$updatedAt":"2024-10-17T12:15:04.549+00:00","$permissions":[],"$databaseId":"6709f300000a8e07a263","$collectionId":"6710f9b5000395f9d3f0"}]}

Note that documentY has an undefined ID and returns a list of documents instead of the created document.

Environment

  • Appwrite version: 1.6.0
  • Server: Self-hosted on Ubuntu 22.04
  • Node.js version: 18.0

Steps to Reproduce

  1. Set up a self-hosted Appwrite instance (version 1.6.0) on Ubuntu 22.04.
  2. Copy the attached code into your function:
  3. Deploy the function to your Appwrite instance.
  4. Execute the function and observe the logs.

Expected Behavior

Both documentX and documentY should be created successfully, with valid IDs and correct data structures returned.

Any insights or suggestions to resolve this issue would be greatly appreciated. Thank you for your help!

TL;DR
Issue arises when creating documents using a Node.js function with Appwrite 1.6.0 and environment variables. Document creation fails silently with environment variables `process.env.APPWRITE_FUNCTION_API_ENDPOINT` and `process.env.APPWRITE_FUNCTION_PROJECT_ID`. Current behavior shows failed creation using environment variables, though successful with hardcoded values. Expected behavior is to create both `documentX` and `documentY` successfully with valid IDs and data structures. **Solution:** Check environment variable setup for correctness and ensure they are properly configured to avoid document creation errors.
ZiaChoudhary
17 Oct, 2024, 12:42

Test Code:

TypeScript
import { Client, Databases, ID } from 'node-appwrite';
export default async ({ req, res, log, error }) => {
try{
    // Initialize the client SDK as clientX
    let clientX = new Client();
    clientX
        .setEndpoint('http://localhost/v1') 
        .setProject('6709f2886E6A86w8') 
        .setKey(req.headers['x-appwrite-key']);

    // Initialize the database SDK as databaseX
    const databaseX = new Databases(clientX);

    //initialize the client SDK as clientY
    let clientY = new Client();
    clientY
        .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
        .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
        .setKey(req.headers['x-appwrite-key']);
    
    // Initialize the database SDK as databaseY
    const databaseY = new Databases(clientY);
    
    //initialize some common IDs
    const collectionId = '6710f9b5000395f9d3f0';
    const databaseId = '6709f300000a8e07a263';

    
    //data to be added to the documentX using databseX
    const dataX = {
        "name": "John Doe",
        "age": 30
    };

    //data to be added to the documentY using databseY
    const dataY = {
        "name": "Jack williams",
        "age": 23
    };

//create a new document in the collection using the databaseX 
    const documentX = await databaseX.createDocument(
        databaseId,
        collectionId,
        ID.unique(),
        dataX
    );
log(`documentX created with ID: ${documentX.$id} and data: ${JSON.stringify(documentX)}`);

    //create a new document in the collection using the databaseY
    const documentY = await databaseY.createDocument(
        databaseId,
        collectionId,
        ID.unique(),
        dataY
    );
log(`documentY created with ID: ${documentY.$id} and data: ${JSON.stringify(documentY)}`);
    return res.json({
        "documentX": documentX,
        "documentY": documentY
    });
    }catch(e){
        error(e.message);
        return res.json({"error": e});
    }
};
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