Rank 3: Container
Hello so I am trying to create something so I can add a attribute to a user and I right now have this code but it doesnt seem to be working:
const sdk = require("node-appwrite");
/* 'req' variable has: 'headers' - object with request headers 'payload' - request body data as a string 'variables' - object with function variables
'res' variable has: 'send(text, status)' - function to return text response. Status code defaults to 200 'json(obj, status)' - function to return JSON response. Status code defaults to 200
If an error is thrown, a response with code 500 will be returned.*/
module.exports = async function (req, res) { const client = new sdk.Client();
// You can remove services you don't use const account = new sdk.Account(client); const avatars = new sdk.Avatars(client); const database = new sdk.Databases(client); const functions = new sdk.Functions(client); const health = new sdk.Health(client); const locale = new sdk.Locale(client); const storage = new sdk.Storage(client); const teams = new sdk.Teams(client); const users = new sdk.Users(client); const userId = req.variables["APPWRITE_FUNCTION_USER_ID"]; //get provided token const token = req.variables["APPWRITE_FUNCTION_DATA"]["token"];
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); } var user = await users.get(userId).catch((err) => { return err; }); //check if user already has a document in the database predictor-db in colllection users var userDoc = await database .listDocuments(`predictor-db`, `users`, [], 1, 0, userId) .catch((err) => { return err; }); // if not create a new one if (userDoc.documents.length == 0) { //create a new document in the database predictor-db in colllection users and set the token await database.createDocument(`predictor-db`, `users`, userId, { token: token, }); } else { //update the token await database.updateDocument( `predictor-db`, `users`, userDoc.documents[0].$id, { token: token, } ); }
res.json({ user: user, });};ERROR:
Error: The document payload is missing. at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Databases.createDocument (/usr/code-start/node_modules/node-appwrite/lib/services/databases.js:1055:16) at async module.exports (/usr/code-start/src/index.js:59:5) at async /usr/local/src/server.js:68:13```