Bug: Appwrite Environment Variables Breaking Document Creation? Need Help!
- 0
- Databases
- Functions
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
andprocess.env.APPWRITE_FUNCTION_PROJECT_ID
), document creation fails silently.
Error Logs
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
- Set up a self-hosted Appwrite instance (version 1.6.0) on Ubuntu 22.04.
- Copy the attached code into your function:
- Deploy the function to your Appwrite instance.
- 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!
Test Code:
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});
}
};
Recommended threads
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Project in AppWrite Cloud doesn't allow ...
I have a collection where the data can't be opened. When I check the functions, there are three instances of a function still running that can't be deleted. The...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...