#Invalid input syntax for type integer

1 messages · Page 1 of 1 (latest)

languid trellis
#

I'm making AfterChange hooks and I stumbled onto weird problem.
I have following snippet producing error from the title

console.log("-----------------------------")
            console.log("Running on: " + doc.name)
            console.log("docsToUpdate")
            console.log(docsToUpdate)
            console.log("dataToUpdate")
            console.log(dataToUpdate)
            console.log("-----------------------------")
            payload.update({
                collection,
                where: {
                    id: {
                        in: docsToUpdate
                    }
                },
                data: dataToUpdate
            })
            payload.update({
                collection,
                where: {
                    id: {
                        not_in: docsToUpdate
                    }
                },
                data: dataToDelete
            })

Console.logs produce:

-----------------------------
Running on: Przystawki
docsToUpdate
[ 12 ]
dataToUpdate
{ category: 1 }
-----------------------------

Full error:

error: Invalid input syntax for type integer: "{"id":1,"name":"Przystawki","dishes":[{"id":12,"name":"1","category":1,"types":[],"updatedAt":"2024-05-17T12:32:16.576Z","createdAt":"2024-05-15T08:55:25.380Z"}],"updatedAt":"2024-05-17T12:32:16.563Z","createdAt":"2024-05-09T17:48:48.402Z"}"

Error comes from node-postgress

daring auroraBOT
languid trellis
#

Full hook (WIP):

import { AfterChangeHook } from 'node_modules/payload/dist/collections/config/types'

type manyToOneRelationshipHookProps = (collection: string, parentFieldName: string, childFieldName: string) => AfterChangeHook

export const manyToOneRelationshipHook: manyToOneRelationshipHookProps = (collection, parentFieldName, childFieldName) => {
    const afterChangeHook: AfterChangeHook = async ({ req, doc, previousDoc }) => {
        const { payload } = req
        const docsToUpdate = doc[parentFieldName].map(d => d.id);
        const dataToUpdate: Record<string, any> = {};
        const dataToDelete: Record<string, any> = {};
        dataToUpdate[childFieldName] = doc.id;
        dataToDelete[childFieldName] = [];
        // If two documents have same values in the array, we don't want to update the child field
        if (doc?.[parentFieldName] !== previousDoc?.[parentFieldName] && (doc?.[parentFieldName]?.length || previousDoc?.[parentFieldName]?.length)) {
            console.log("-----------------------------")
            console.log("Running on: " + doc.name)
            console.log("docsToUpdate")
            console.log(docsToUpdate)
            console.log("dataToUpdate")
            console.log(dataToUpdate)
            console.log("-----------------------------")
            payload.update({
                collection,
                where: {
                    id: {
                        in: docsToUpdate
                    }
                },
                data: dataToUpdate
            })
            payload.update({
                collection,
                where: {
                    id: {
                        not_in: docsToUpdate
                    }
                },
                data: dataToDelete
            })
        }
    }
    return afterChangeHook;
}