简体   繁体   中英

Module works in development but not in heroku: nodejs and hbs handlebars

UPDATED QUESTION:

In local development, modules are working. When I push to heroku, one module looks like it is not working, it is not passing variables to the helper.

My helpers folder:

ad.helper.js
car.helper.js
pagination.helper.js
urlPagination.helper.js

pagination.helper.js contains:

const hbs = require('hbs');
const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', paginate);

some alert is in require('halndlebars-paginate')

module "/home/santiago/code/folder/projectName/node_modules/handlebars-paginate/index"
Could not find a declaration file for module 'handlebars-paginate'. '/home/santiago/code/folder/projectName/node_modules/handlebars-paginate/index.js' implicitly has an 'any' type.
  Try `npm install @types/handlebars-paginate` if it exists or add a new declaration (.d.ts) file containing `declare module 'handlebars-paginate';`ts(7016)

View where helper is => lis.hbs, that contains:

 {{#paginate pagination type="first"}}
      <a {{#if disabled}}class="disabled"{{/if}} 
      href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Primera</li></a>
    {{/paginate}}
    {{#paginate pagination type="previous"}}
      <a {{#if disabled}}class="disabled"{{/if}} 
      href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Menos</li></a>
    {{/paginate}}
    {{#paginate pagination type="middle" limit="5"}}
      <a {{#if active}}class="active"{{/if}} href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li >{{n}}</li></a>
    {{/paginate}}
    {{#paginate pagination type="next"}}
      <a {{#if disabled}}class="disabled"{{/if}} href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Más</li></a>
    {{/paginate}}
    {{#paginate pagination type="last"}}
      <a {{#if disabled}}class="disabled"{{/if}} href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Última</li></a>
    {{/paginate}}

When I console.log parentCategory in controller, I saw it, BUT if I console.log in urlpagination.helper it is undefined.

So, I think this module is not working in production (heroku), but I can't understand why it is in development.

OLD question: Something is happening with this module I can't figured out for some days.

It is this npm package:

https://github.com/olalonde/handlebars-paginate

I am using handlebars. Inside my folder for helpers, I have:

const hbs = require('hbs');
const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', paginate);

In my text editor I can see this warning:

Could not find a declaration file for module 'handlebars-paginate'. '/home/santiago/code/folder/nameProject/node_modules/handlebars-paginate/index.js' implicitly has an 'any' type.
  Try `npm install @types/handlebars-paginate` if it exists or add a new declaration (.d.ts) file containing `declare module 'handlebars-paginate';`

I tried to do both solutions.

First one, throw errors, so I created adts file, and warning go away. After that,still not working in heroku (production mode).

I am NOT using typescript, I think this second solution is not for me, why this warning? (Note: when i created this file, it didn't upload to heroku, even when I tried many times looking for gitignore...)

Funny is, it is working absolutely perfect in development (localhost), but in heroku, it doesn't work just this module - pagination.

What am I doing wrong? Why it looks like TS error?

I am very newbie. PLEASE HELP, I am pretty close to finish my first project!

I solved it. I think it is a extremely stupid mistake, about paths in require modules . Being more accurate, the problem was here:

My helpers folder:

ad.helper.js
car.helper.js
pagination.helper.js
urlPagination.helper.js

Pagination module, was require in pagination.helper.js:

const hbs = require('hbs');
const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', paginate);

require('handlebars-paginate') goes through node_modules => index.js:

module.exports = function(pagination, options) {
  var type = options.hash.type || 'middle';
  var ret = '';
  var pageCount = Number(pagination.pageCount);
  var page = Number(pagination.page);
  var limit;
  var state = Number(pagination.state);
  var parentCategory = Number(pagination.parentCategory);
  var category = Number(pagination.category);
  var searchWord = pagination.searchWord;
  var vendor = pagination.vendor;
  var ageLow = Number(pagination.ageLow);
  var ageHigh = Number(pagination.ageHigh);
  var vendorType = pagination.vendorType;
  var priceLow = pagination.priceLow;
  var priceHigh = pagination.priceHigh;
  var brand = pagination.brand;
  var carmodel = pagination.carmodel;
  var yearLow = pagination.yearLow;
  var yearHigh = pagination.yearHigh;
  var km = pagination.km;
  var ccLow = pagination.ccLow;
  var ccHigh = pagination.ccHigh;


  if (options.hash.limit) limit = +options.hash.limit;

  //page pageCount
  var newContext = {};
  switch (type) {
    case 'middle':
      if (typeof limit === 'number') {
        var i = 0;
        var leftCount = Math.ceil(limit / 2) - 1;
        var rightCount = limit - leftCount - 1;
        if (page + rightCount > pageCount)
          leftCount = limit - (pageCount - page) - 1;
        if (page - leftCount < 1)
          leftCount = page - 1;
        var start = page - leftCount;

        while (i < limit && i < pageCount) {
          newContext = { n: start, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (start === page) newContext.active = true;
          ret = ret + options.fn(newContext);
          start++;
          i++;
        }
      }
      else {
        for (var i = 1; i <= pageCount; i++) {
          newContext = { n: i, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (i === page) newContext.active = true;
          ret = ret + options.fn(newContext);
        }
      }
      break;
    case 'previous':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page - 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'next':
      newContext = {};
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page + 1 , state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh}
      }
      ret = ret + options.fn(newContext);
      break;
    case 'first':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'last':
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
  }

  return ret;
};

I don't know why, this works in local development, but NOT in production (heroku)... I would like to understand it, if someone knows to answer.

So,Ifixed by adding this function to my helper like:

const hbs = require('hbs');
// modified for production env - 
// const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', function(pagination, options) {
  var type = options.hash.type || 'middle';
  var ret = '';
  var pageCount = Number(pagination.pageCount);
  var page = Number(pagination.page);
  var limit;
  var state = Number(pagination.state);
  var parentCategory = Number(pagination.parentCategory);
  var category = Number(pagination.category);
  var searchWord = pagination.searchWord;
  var vendor = pagination.vendor;
  var ageLow = Number(pagination.ageLow);
  var ageHigh = Number(pagination.ageHigh);
  var vendorType = pagination.vendorType;
  var priceLow = pagination.priceLow;
  var priceHigh = pagination.priceHigh;
  var brand = pagination.brand;
  var carmodel = pagination.carmodel;
  var yearLow = pagination.yearLow;
  var yearHigh = pagination.yearHigh;
  var km = pagination.km;
  var ccLow = pagination.ccLow;
  var ccHigh = pagination.ccHigh;


  if (options.hash.limit) limit = +options.hash.limit;

  //page pageCount
  var newContext = {};
  switch (type) {
    case 'middle':
      if (typeof limit === 'number') {
        var i = 0;
        var leftCount = Math.ceil(limit / 2) - 1;
        var rightCount = limit - leftCount - 1;
        if (page + rightCount > pageCount)
          leftCount = limit - (pageCount - page) - 1;
        if (page - leftCount < 1)
          leftCount = page - 1;
        var start = page - leftCount;

        while (i < limit && i < pageCount) {
          newContext = { n: start, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (start === page) newContext.active = true;
          ret = ret + options.fn(newContext);
          start++;
          i++;
        }
      }
      else {
        for (var i = 1; i <= pageCount; i++) {
          newContext = { n: i, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (i === page) newContext.active = true;
          ret = ret + options.fn(newContext);
        }
      }
      break;
    case 'previous':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page - 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'next':
      newContext = {};
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page + 1 , state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh}
      }
      ret = ret + options.fn(newContext);
      break;
    case 'first':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'last':
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
  }

  return ret;
})

I tried fixed before with relatives paths:

./
../
node_modules/

But it doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM