Thursday 4 May 2017

How to replace nested .then() function in bookshelf

I am new to node.js and I am having trouble with removing multiple nested .then() inside of a bookshelf.js function.

The code below is what I am working on right now. And it works fine for now but I need to add more than 20+ nested .then() to finish the project. Therefore, I am trying to replace .then() to some other method before it gets crazy.

Any help or tip would be appreciated.

   getItem: function (req, res, next) {

        DepartmentCollection.forge()
        .fetch({
              debug: true     
        })
        .then(function(collection) {
             new GlossaryTerm({'GlossaryTermID': req.params.id}).fetch({
                    withRelated: ['department'],
                    debug: true     
           })
              .then(function(model) {
                    if ("undefined" === typeof collection) { console.log("step 2: variable is undefined") }
                    else { console.log("step 2: variable is defined") };

                    res.render('glossary/glossary-term-detail',{domain:'GlossaryTerm', title: 'Glossary Term Detail',
                                                                  data: model, department_data: collection }); 

              }).catch(function(error) {
                    console.log(error);
                    res.send('An error occured');
              }); 

        }).catch(function(error) {
              console.log(error);
              res.send('An error occured');
        });

  }



via cokrgj

No comments:

Post a Comment