Node library implementation

Node library implementation

DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

Hi Allan

We are implementing editor with nodejs. The current versions that we are using are:

Nodejs: 9.5
knex: 1.4

We had installed datatables.net-editor-server from npm repositorys

The code of node is next

layer service

import db from '../../models/db';
const DBTABLE = 'schema.table_name'; 
import {
    Editor,
    Field,
    Validate,
    Format,
    Options
}  from 'datatables.net-editor-server';


        async proccessAll(req, eliminado=false) {
            try {
                let editor = new Editor(db, DBTABLE).fields(
                    new Field('arl_nombre'),
                    new Field('arl_tipo'),
                    new Field('arl_vigente'),
                    new Field('arl_defecto')
                );
                return await editor.process(req.body);                
            } catch (error) {
                throw error;
            }
        };

layer conrtoller

router.all('/proccess', async (req, res, next) => {
    try {
        //procesando la información...
        const editor = await aerService.proccessAll(req);
        //retornando...
        res.status(200).json(editor.data());        
    } catch (error) {
        res.status(400).json({
            error: error.message
        });
    }
});

Is the exactly the examples of Node documentation, but, always we have this message: Cannot convert undefined or null to object

Please, can yoy help us?

Thanks a lot for you support

«1

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I presume you've removed some other code for brevity? For example:

    async proccessAll(req, eliminado=false) {

    Is in a class I guess?

    Can you show me the backtrace from the error? If you remove the try / catch that should show more error information.

    Also does the error happen when you load the table, add a new row, edit or delete?

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Some ones additional software that we are using
    Postgres: 9.5
    Express: 4

    Yes, It's a class. We are using a service layer for bussines control. But, this code was passed to controller layer and happened the same situation, following the exactly example of node documentation

    Well, the code is next:

    export default class AerolineaService {
            /*****
             */
            async proccessAll(req, eliminado=false) {
                try {
                    let editor = new Editor(db, DBTABLE).fields(
                        new Field('arl_nombre'),
                        new Field('arl_tipo'),
                        new Field('arl_vigente'),
                        new Field('arl_defecto')
                    )
                    .where('eliminado', eliminado);
                    //proccess data...
                    return await editor.process(req.body);                
                } catch (error) {
                    throw error;
                }
            };
    };
    

    It's happen when we want to make a insert, update, delete, but for reading it's all ok

    Sure, the trace of try catch trigger this information:

    (node:6699) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
        at Function.keys (<anonymous>)
        at Editor.<anonymous> (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.ts:1364:23)
        at step (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:42:23)
        at Object.next (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:23:53)
        at /Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:17:71
        at new Promise (<anonymous>)
        at __awaiter (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:13:12)
        at Editor._process (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:1086:16)
        at /Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.ts:700:16
        at step (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:42:23)
        at Object.next (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:23:53)
        at /Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:17:71
        at new Promise (<anonymous>)
        at __awaiter (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:13:12)
        at run (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:409:36)
        at Editor.<anonymous> (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.ts:712:10)
        at step (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:42:23)
        at Object.next (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:23:53)
        at /Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:17:71
        at new Promise (<anonymous>)
        at __awaiter (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:13:12)
        at Editor.process (/Developments/NodeProjects/pasajesNode/node_modules/datatables.net-editor-server/dist/editor.js:402:16)
    (node:6699) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
    (node:6699) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    

    Thanks for your help
    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Really we were implementing in diferents forms to apply the examples with editor server. But in all ways shows those error before metioned.

    Other point that forgot to tell you is that we are using with views engine.

    Pug, handelbars or pure html, but we had those error in all of kind of views

    Please, give us your advices

    Waiting for you support

    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    This is the line that is throwing the error:

    let keys = Object.keys( data.data );
    

    Immediately before you call editor.process, could you add:

    console.log( JSON.stringify( req.body ) );
    

    and let me know the result that is printed out on the console?

    I'm wondering if extended: true is missing where you use body parser - e.g.:

    app.use( bodyParser.urlencoded({ extended: true }) );
    

    Regards,
    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Thanks, you gived us a track.

    The problem was in the bodyParse line... but in express give a similar line setting like this:

    app.use(express.urlencoded({ extended: fasle })); 
    

    changed to:

    app.use(express.urlencoded({ extended: true }));
    

    and finally works perfect

    Thanks a lot for your help
    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    We are implementing editor full nodejs for a project, it's genial to fast developtment.

    One more question, in the documentation give us a closer view to make sql oriented to conextion one DB... but we have to combinate columns of two DB in one table... Can we make this kind of joins?

    The examples show to make joins of one especific DB. In our case, as I mentioned, we need to cross of other columns in distinct DB

    Thanks for your help

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    This example shows two db fields being shown in a single DataTables column. Is that what you mean?

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Well exactly that we are looking for the mehod that the editor allow literal o something similar, example

    Editor(db, 'schema.table_name').fields(
    new Field('id')
    new Fied('field_2')
    new Field('SELECT array_agg(db2.table.field_name) AS filed_3 FROM db2.table AS t2 ON t2.id = id')
    );
    ...
    

    This kind of union we have the job using other orm that allow make sql string as a field, but our idea is to use full Editor library.

    We still reading the documentation, but if you had a similar proble, anda you have the answer it would be a lot of help.

    Thanks for you support
    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Hi,

    Yes, I see what you mean. Unfortunately, at the moment in the NodeJS libraries for Editor, it doesn't support raw SQL statements in the field name. Some kind of ability to do that is something that I'm considering for the Node, PHP and .NET libraries, but haven't yet nailed that down.

    Regards,
    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    We hope that this option can implemented in the next version, would be a lot of help
    Thanks for you support

    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    It's me again, sorry -but is good news for all of you- we are implementig node library

    Question
    We're validating data with custom message, but nor in the examples can't had put the custom message, always appear: 'Input no valid'

    We have followed the indications:

    new Field('last_name').validator(Validate.notEmpty(),
    Validate.Options({
    message: "This is the custom message"
    })),
    

    This alway show 'Input not valid', give us a help

    Thanks
    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    In the crud option, we don't have problems proccesing data, except with the delete option.
    We are implementig a REST interface and following the node documentation explain, but, this method never processing

    What is the cause?
    We forgot something?

    Here is the example in the class implementation:

        async remove(req) {
            try {
                let editor = new Editor(dbPreRep, PRES_DB_TABLE).fields(
                    new Field(this.eliminado).setValue(true),
                    new Field(this.ip_update).setValue(NetWorkService.getClientIP(req)),
                    new Field(this.fecha_upd).setValue(new Date)
                )
                .on('preRemove', async(editor, id, values) => {
                    console.log('Deleting, but never enter here....');
                });
                //proccess data...
                return await editor.process(req.body);
            } 
            catch (error) {
                throw error;    
            }
        };
    

    We need to make a logical deletion, is the cause that we want to update the fields. We made a code trace, but never enter in the line of console.log.

    This is the js to client level

    var getRestOption = function() {
        return {
            create: {
                type: 'POST',
                url: '/path/save'
            },
            edit: {
                type: 'PUT',
                url: '/path/edit/_id_'
            },
            remove: {
                type: 'DELETE',
                url: '/path/remove/_id_'
            }
        }; 
    }
    
    var initEditor = function() {
        editor = new $.fn.dataTable.Editor({
            ajax: getRestOption(),
            table: "#tableId",
            fields: getFields()
        });
    }
    

    As we mentioned to you, read, create, update there are no problem with those, the problem existing is in the delete option.

    Thanks for your help
    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Your Validate.Options should be passed in as an option to the validator (notEmpty in this case):

    new Field("last_name").validator(
      Validate.notEmpty(
        Validate.Options({
          message: "This is the custom message"
        })
      )
    );
    

    Validation documentation is here.

    Regarding the delete, depending n what your server is expecting, you might need to use ajax.deleteBody.

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Thanks for you reply

    Logical deletion was made with a few changes. It's working
    Message validation, doesn't work. We had putted the code as you had mentioned and doesn't work.
    Even in the code example of nodejs library that we have to make testing

    We had made testing, but it's not working, not with nonEmpty validation, this happen with other validations. Here is the validation that we try to use:

                    new Field(this.field)
                    .validator(Validate.minNum(
                        5,
                        new Validate.Options({
                            message: "Field need minim 5 characters"
                        })
                    ))
    

    And the same way, editor response: 'Input not valid'

    Please check if we are failing or something happen whit the editor code

    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    minNum checks that a numeric value is submitted - in the case above it will check for a number less than 5 and fail if found.

    Its signature is actually:

    minNum(min, decimal='.', cfg=null: ValidationOptions) 
    

    I missed the decimal parameter in the documentation which is why your example above isn't working. I'll get that fixed right now!

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    We still with this problem. The validation option doesn't show the custom message.
    We putted a custom validator and the message show ok, but with the Validate.Options the message doesn't work and always show 'Input not valid'

    Here is other validation option that we try to use:

                    new Field(this.igo_titulo).validator(
                        Validate.minLen(5, 
                            new Validate.Options({
                                message: 'Mensaje de validación...'
                            })
                        )
                    )
    

    Thanks for your help

    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Other cuestion, now over join tables.

    Can we make a join tables not based in id column, that is, choose other column that not be colum id?

    Example:

    leftJoin('other_table', 'cod_field', '=', table.cod_field)
    

    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Hi,

    Apologies for my confusion about the validation issue. I see the problem now and have committed a fix. I'm going to tag and release 1.7.4 which will contain this fix soon (this week).

    Regarding your join question - yes you can join on other columns. For performance reasons it is best to join on indexed columns, so you might need to add an index, but that should work fine.

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    With respect to leftJoin method, we follow step by step the indications of node library to make a join table and as we commented to you, we need join other fieds that not be exactly 'id' field.

    But we always recibed this message:

    "Table part of the field "id" was not found. In Editor instances that use a join, all fields must have the database table set explicitly."
    

    The sql example that we try to arm is next:

                let editor = new Editor(dbPreRep, PRES_DB_TABLE)
                    .fields(
                        new Field(this.id),
                        new Field(this.igo_region1),
                        new Field(this.igo_region2)
                        ...
                    )
                    .leftJoin('table_region as reg1', 'reg1.dip_codigo', '=', PRES_DB_TABLE + '.' + this.igo_region1)
                    .where(this.active, active);
    

    Give us your help, many thanks

    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I think it is the alias that is causing the issue. Could you try removing the as reg1 part please (and updating the references to that table)?

    If that doesn't work, can you show me your full initialisation.

    Thanks,
    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Thanks Allan

    We are advancing in our projet with nodejs and editor...

    Just one more question... in reference to uploadfiles. It tokes some time to resolve delete files. In the interface you putt a button with X to remove files, but it is not attach a event that we can controlled remove each one file.

    To resolve this inconvenent, we putted in the event of field upload a event that can controll it. To resume this the little code

    //every time we upload always set a evento to attach to button X to remove file
    $(editor.field('fiels').input()).on('upload.editor', function(e, val) {
     $('button.remove').click((e)=> {
        let id = $(e.currentTarget).closest('li').find('a').attr('data-value');
       //continue with the code to delete a file by id
     });
    })
    

    We made this kind of code because the event dessapear every time that one file is uploaded, and we need to set again the event for that not dessapear.

    The cuestion is, It's correct that we are making?, can you put a event directly to the button X to remove file?

    Thanks for your support
    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    In the interface you putt a button with X to remove files, but it is not attach a event that we can controlled remove each one file.

    The reason for that is that the files information isn't sent to the server until the form is submitted. Only at that time do you know if a file can fully be deleted. E.g. consider two users using the same table - one deletes and image and the other reloads the table. The reference to the file would still be in the original row if it hasn't yet been submitted, but the file has been removed, resulting in an error.

    Editor uses the concept of orphaned files to handle this.

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    We are trying to implement datatables and editor in client framework, especifctly using ionic 3. As you know, this framework it's make under angular

    After some investigations to implemente, we resolved to problem for shows data in the datatable view

    import jQuery from 'jquery';
    import 'datatables.net';
    
    export class OurPage {
     ionViewDidLoad() {
        this.table = jQuery('#idTable');
        this.table.DataTable({
    ajax: 'url/to/list',
    select: true
    });
     }
    }
    

    and works perfectly.... but with the editor present a problem.
    First, we follow the instructions en readme file to install de editor zip file, but when we try to call from component ts the editor shows a error that indicate: jQuery is not defined

    import jQuery from 'jquery';
    import 'datatables.net';
    import 'datatables.net-editor';
    
    export class OurPage {
     ionViewDidLoad() {
        this.table = jQuery('#idTable');
        this.table.DataTable({
    ajax: 'url/to/list',
    select: true
    });
     }
    }
    

    Please, give us a help, thanks

    Regards

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Does it throw that error on this.table = jQuery('#idTable'); or somewhere else? Can you link to a page showing the issue please.

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Sorry, we are working in localhost server our project and couldn't putted in internet... we almost finish but the editor gived us a little trouble.

    Exactly this is that happen

    import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams, Config } from 'ionic-angular';
    
    import 'datatables.net-select-bs4';
    import 'datatables.net-editor-bs4'; //-> this line will trigger the error that say 'jQuery is not defined'
    /**
     * Generated class for the AerolineaPage page.
     *
     * See https://ionicframework.com/docs/components/#navigation for more info on
     * Ionic pages and navigation.
     */
    
    @IonicPage()
    @Component({
      selector: 'page-aerolinea',
      templateUrl: 'aerolinea.html',
    })
    export class AerolineaPage {
    
        private editor: any;
        private table: any;
        private oTable: any;
    
      constructor(public navCtrl: NavController, 
        public navParams: NavParams, 
        public config:Config) {
      }
    
      private getColumns() {
        return [
            {            
                title: 'Name',
                data: 'aer_name'
            }
        ]
      };
    
      onInitEventsDt() {
          this.oTable.on('click', 'tr', (e) => {
            let data = this.oTable.row(e.currentTarget).data();
            console.log(data);
          });
      }
    
      ionViewDidLoad() {
        this.table = $('#tAerolinea');
        this.oTable = this.table.DataTable({
            ajax: 'http://dirserver/aerolinea/list',
            select: true,
            columns: this.getColumns()
        });
        //init events Dt...
        this.onInitEventsDt();
      }
    
    }
    

    As we sayed to you, we can't call to the editor althoug we made a installation of and editor node_modules packages.

    Datatables worlks perfectly

    Thanks for your help

    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    We remove those lines:

    import 'datatables.net-editor-bs4';
    

    or

    import 'datatables.net-editor'; 
    

    and the code runs without errors.

    But if we put the lines of above, the page shows the next message:
    ```

    Runtime Error
    jQuery is not defined
    Stack
    ReferenceError: jQuery is not defined
        at http://localhost:8101/build/vendor.js:138327:21
        at Object.<anonymous> (http://localhost:8101/build/vendor.js:136729:11)
        at __WEBPACK_AMD_DEFINE_ARRAY__ (http://localhost:8101/build/vendor.js:136730:5)
        at Object.<anonymous> (http://localhost:8101/build/vendor.js:136752:2)
        at __webpack_require__ (http://localhost:8101/build/vendor.js:55:30)
        at http://localhost:8101/build/vendor.js:163236:89
        at Object.<anonymous> (http://localhost:8101/build/vendor.js:163263:2)
        at __webpack_require__ (http://localhost:8101/build/vendor.js:55:30)
        at Object.100 (http://localhost:8101/build/main.js:12:84)
        at __webpack_require__ (http://localhost:8101/build/vendor.js:55:30)
    
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    What if you use:

    import 'datatables.net-editor';
    import 'datatables.net-editor-bs4';
    

    Does that work? Or does that also give the same error?

    Allan

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Hi Allan

    Yes, we putted and trigger the same error

    Perhaps, we are forgot to put something in the ionic config json file or something else that we lost, and that you have an idea?

    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    Well, finally works but without calling editor on the ts class

            this.editor = new $.fn.DataTable.Editor({
                ajax: this.getRestAjaxAerolinea(),
                table: this.tableId,
                fields: this.getFields(),
                template: '#form-informe'    
            });
    

    Putting this code works editor.

    Thanks for you help

    Regards

  • DesarrolloPRECDesarrolloPREC Posts: 34Questions: 4Answers: 0

    Allan

    The other post was lost or do you have some kind of administration?

    Thanks

This discussion has been closed.