Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting Error When Trying to Update a Model Using Objection

I am getting this error while trying to update a record in my model using objection.js.

The error I am getting is

Error: An error occurred while executing $setJson method. $setJson method was given an invalid value 1966
at setJson (node_modules/objection/lib/model/modelSet.js:13:11)
at UserDevices.$setJson (node_modules/objection/lib/model/Model.js:128:12)
at UserDevices.fromJson (node_modules/objection/lib/model/Model.js:264:11)
at UserDevices.ensureModel (node_modules/objection/lib/model/Model.js:584:25)
at UpdateOperation.onAdd (node_modules/objection/lib/queryBuilder/operations/UpdateOperation.js:21:29)
at QueryBuilder.callOperationMethod (node_modules/objection/lib/queryBuilder/QueryBuilderOperationSupport.js:382:33)
at QueryBuilder.addOperationUsingMethod (node_modules/objection/lib/queryBuilder/QueryBuilderOperationSupport.js:413:28)
at QueryBuilder.addOperation (node_modules/objection/lib/queryBuilder/QueryBuilderOperationSupport.js:404:22)
at /home/node/app/node_modules/objection/lib/queryBuilder/QueryBuilder.js:709:12
at writeOperation (node_modules/objection/lib/queryBuilder/QueryBuilder.js:1317:5)
at QueryBuilder.patch (node_modules/objection/lib/queryBuilder/QueryBuilder.js:706:12)
at UserDevicesConsumer.handleKafkaMessage (lib/kafka/consumers/userDevices.js:61:47)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Context.<anonymous> (test/kafka/consumer/userDevices.test.js:95:9)

This is the code I am using to update the model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

await UserDevices.query().patch(device.id, {deleted_at: new Date().toISOString()});

and this is the schema of my model

const { Model } = require('objection');

class UserDevices extends Model {
    static get tableName() {
        return 'user_devices';
    }

    static get idColumn() {
        return 'id';
    }

    static get jsonSchema() {
        return {
            type: 'object',
            properties: {
                id: { type: 'number' },
                device_id: { type: 'number' }
            }
        };
    }
}

module.exports = UserDevices;

I have spent hours trying to figure this out but to no avail

>Solution :

You are getting an error because the patch method expects one parameter which should be an object.

Change

await UserDevices.query().patch(device.id, {deleted_at: new Date().toISOString()});

to

await UserDevices.query().findById(device.id).patch({deleted_at: new Date().toISOString()});

and that should fix the issue

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading