coscinoOriginal post
Rank 2: Process
This is my nodejs, cloud function:
JavaScript
import { Client, Databases, ID, Permission, Role } from 'node-appwrite';
export default async ({ req, res, log, error }) => {
const client = new Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject(process.env.APPWRITE_PROJECT_ID) .setKey(process.env.APPWRITE_API_KEY);
const databases = new Databases(client);
// You can log messages to the console log('Hello, Logs!');
// If something goes wrong, log an error error('Hello, Errors!');
// Parse the request body to get IDINPUT const { IDINPUT } = JSON.parse(req.body); log(IDINPUT);
if (req.method === 'GET') { // Send a response with the res object helpers return res.send(`hello ${process.env.DOG_NAME}`); } else if (req.method === 'POST') { try { /// Create a document in the specified collection const response = await databases.createDocument( process.env.APPWRITE_DATABASE_ID, process.env.APPWRITE_USERINFO_ID, ID.unique(), {surname: 'dibba'}, [ Permission.write(Role.user(IDINPUT)), Permission.read(Role.user(IDINPUT)), ] );
console.log(response); return res.send('document created');
} catch (err) { console.log(err); return res.send('document not created, internal error'); } }
return res.json({ motto: 'Build like a team of hundreds_', learn: 'https://appwrite.io/docs', connect: 'https://appwrite.io/discord', getInspired: 'https://builtwith.appwrite.io', });};I know I am parsing IDINPUT correctly because in the log I see the correct user id.
I call this function in my flutter app, where I am logged in with an active email session.
I have document level permission active on my collection. I have set that any user can just create.
My apikey has 2 scopes:
- read documents
- write documents
Summary
Error message indicates the permissions should be "any" or "guests," not "user" in the function. The solution is to update the function to use `Permission.write(Role.any())` and `Permission.read(Role.any())` instead of `Permission.write(Role.user(IDINPUT))` and `Permission.read(Role.user(IDINPUT))`.