Skip to content
Back

Create document fails in production function

  • 0
  • Functions
  • Web
Pabin Dhami
27 Oct, 2024, 08:31

I'm trying to create a document from my appwrite function with and it works perfectly fine when i execute the function in local, but in production function instead of creating document and giving the new document response it is giving me the document list,

TL;DR
Developers are experiencing an issue where creating a document in the production function returns a list of documents instead of the new document. The suggested solution is to update the node sdk version to `14.1.0` for `node-appwrite`, remove the `node_modules`, and then perform a `npm install` for a clean initialization.
darShan
27 Oct, 2024, 08:33

Whats the code? Maybe also post the error logs if available.

Pabin Dhami
27 Oct, 2024, 08:36

no it gives success reponse, but the reponse contains all the document list instead of the new document

darShan
27 Oct, 2024, 08:36

can you show some relevant code?

Pabin Dhami
27 Oct, 2024, 08:37

this is when i execute local function

Pabin Dhami
27 Oct, 2024, 08:37

this is when i execute remote function

Pabin Dhami
27 Oct, 2024, 08:38
Pabin Dhami
27 Oct, 2024, 08:38

this is the code

Pabin Dhami
27 Oct, 2024, 08:38
TypeScript
const { user } = req;
        const { db } = appwriteClient(req, res);
        const requiredFields = ['documentOf', 'documentType'];
        const {
            documentOf,
            entityId,
            documentType,
            identifiedBy,
            identificationNo,
            VIN,
            description,
            category,
            status,
            amount,
            address,
            issuedBy,
            issuedDate,
            expiryDate,
            renewalDate,
            notes,
            fileIds
        } = req.body;

        if (!user || !user.id) {
            return res.json({ message: "User ID is required" }, 400);
        }

        for (const field of requiredFields) {
            if (!req.body[field]) {
                return res.json({ message: `${field} is required` }, 400);
            }
        }

        const newData = {
            documentOf,
            entityId: entityId || null,
            documentType,
            identifiedBy: identifiedBy || null,
            identificationNo: identificationNo || null,
            VIN: VIN || null,
            description: description || null,
            category: category || null,
            status: status || null,
            amount: amount || null,
            address: address || null,
            issuedBy: issuedBy || null,
            issuedDate: issuedDate || null,
            expiryDate: expiryDate || null,
            renewalDate: renewalDate || null,
            notes: notes || null,
            fileIds: fileIds || [],
            userId: user.id.toString()
        };

        const response = await db.createDocument(Env.dbId, Env.collectionId, ID.unique(), newData);

        return res.json({ message: "Document created successfully", document: response }, 201);
darShan
27 Oct, 2024, 08:39

please use 3 backticks to format the code

Pabin Dhami
27 Oct, 2024, 08:39

its done

darShan
27 Oct, 2024, 08:43

are you using cloud or self hosted? whats the sdk version used?

Pabin Dhami
27 Oct, 2024, 08:43

i'm using the package called @darShan/appexpress for express like routing in appwrite function

darShan
27 Oct, 2024, 08:44

lol thats my library. but I mean what appwrite sdk? appwrite, node-appwrite and the version.

Pabin Dhami
27 Oct, 2024, 08:45
Pabin Dhami
27 Oct, 2024, 08:46

wow, i think i met the right guy for my error

darShan
27 Oct, 2024, 08:47

the latest version is 14.1.0 for node-appwrite. can you check if that helps. different errors on same code is tricky. Also, remove the node_modules and then do a npm install for a clean init.

Pabin Dhami
27 Oct, 2024, 08:47

lol the @ name automatically updated to your name

darShan
27 Oct, 2024, 08:47

also the appexpress version is pretty outdated too.

Pabin Dhami
27 Oct, 2024, 08:48

i don't think this issue is due to appexpress

darShan
27 Oct, 2024, 08:48

indeed.

Pabin Dhami
27 Oct, 2024, 08:50

do you know any other pakage for suggestion which is better than appexpress?

darShan
27 Oct, 2024, 08:52

I am biased but any other package that support custom view engines [HBS, PUG, JSX, TSX, etc], has middleware and http web compression support, not that I know of. But there were a few hono like adapters I think. Maybe check #🛠│tools or #🎪│showcase.

darShan
27 Oct, 2024, 08:52

also, did you try updating the node sdk version as I suggested above?

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