Custom helper with handlebars-loader - helper

I'm using this handlebars-loader for webpack 4. Now I wanted to use a custom helper, but i only get this error
ERROR in Template execution failed: TypeError: __default(...).call is not a function
ERROR in TypeError: __default(...).call is not a function
this is my webpack.config
//handlebars-loader
{
test: /\.(hbs)$/,
loader: "handlebars-loader",
options: {
helperDirs: [path.join(__dirname, './src/hbs/helpers')],
partialDirs: [path.join(__dirname, './src/hbs/partials')]
},
}
and this is my simple helper
const Handlebars = require('handlebars');
Handlebars.registerHelper('repeat', function(n) {
console.log(n);
});
and how I use it
{{#repeat 10}}
<span> repeat</span>
{{/repeat}}
Does anyone know what I'm doing wrong?

I found a solution. I have to export the helper as a module, then it works.
//repeat.js
module.exports = function(times, options) {
console.log(times);
};

Thank you, Gregor! This has been frustrating me all day!
I had a simple function in a helper file, checkActive.js :
const Handlebars = require("handlebars")
Handlebars.registerHelper("checkActive", function (pageName, linkedPageName) {
return pageName === linkedPageName ? "active" : ""
})
This would not work and gave me this error:
ERROR in Template execution failed: TypeError: __default(...).call is not a function
ERROR in TypeError: __default(...).call is not a function
I found your solution and tried it. Lo-and-behold it worked! This is what I am now using instead:
module.exports = function (pagename, linkedPageName) {
return pagename === linkedPageName ? "active" : ""
console.log(pagename)
};
In order for this to work, you need to have the function saved in a file with the same name as the handlebars helper function you are calling, ie. in my case: checkActive.js

Related

Can't export multiple modules [ExpressJs]

this my error: Route.get() requires a callback function but got a [object Object]
module.exports = getAccessToRoute;
I don't get an error when I export as,
But
module.exports = { getAccessToRoute, getAdminAccessToken };
when i export like this i get error.
I don't have problem nother middleware.
I forgot the parentheses when calling from within another router.
const getAccessToRoute = require('../middlewares/authorization/auth');
i fixed it like this
const { getAccessToRoute } = require('../middlewares/authorization/auth');

Why module.exports does not export a function properly?

I'm building a project based on CJ's from Coding Garden Inventory App. In a knex migration file I have used an external file to bring a helper functions.
tableUtils.js
function addDefaultColumns(table) {
table.timestamps(false, true)
table.datetime('deleted_at')
}
function createNameTable(knex, tableName) {
return knex.schema.createTable(tableName, table => {
table.increments().notNullable()
table.string('name').notNullable().unique()
addDefaultColumns(table)
})
}
module.exports = {
createNameTable,
addDefaultColumns
}
and in my migration file:
const tableNames = require('../../src/constants/tableNames');
const { createNameTable, addDefaultColumns } = require('../../src/constants/tableNames');
exports.up = async (knex) => {
await knex.schema.createTable(tableNames.user, table => {
table.increments().notNullable()
table.string('name').notNullable()
table.string('email', 254).notNullable().unique()
table.string('password', 127).notNullable()
table.string('avatar_url', 2000)
table.string('color', 15).defaultTo('#dddddd')
table.specificType('balance', 'money').defaultTo('0')
addDefaultColumns(table)
})
};
Once tryint to run migration with knex migrate:latest I am getting error:
migration failed with error: addDefaultColumns is not a function
addDefaultColumns is not a function
TypeError: addDefaultColumns is not a function
What am I missing here as it looks like everything should work fine.. The function is declared with function and above module.exports so there shouldn't be a problem of function being undefined..
Your code shows you requiring tableNames, but you show a file named tableUtils.js so it appears you aren't requiring the right file.

SINON unittesting stub constructor

I'm trying to stub a constructor in node but I really can't.
I found this , that is quite similiar to what I need to do but I have an error that I could not solve.
//file.js
var foo = require('foo-client')
function toTest () {
var bar = foo()
returns config = bar.foo2(a,b) // returns a Promise
}
what I am trying to do in the test file is
//file-sepc.js
var stub = sinon.stub()
stub.returns(Promise.resolve('config'))// config it's just an example
var file = proxyquire('./file.js', {
'foo-client':{foo2: stub}
})
file.toTest()
.then((result) => {
console.log(result)
done()
})
supposing the node syntax is correct, I am getting this output:
TypeError: foo is not a function
Can anyone help me telling me where is my error or another way to mock/stub this things?
Thanks a lot!
Haven't tried running your code but it looks like foo-client should be a function rather than an object in order for the var bar = foo() not to throw an error you are seeing. Try the following:
var file = proxyquire('./file.js', {'foo-client': sinon.stub.returns({ foo2: stub }) })

Express4 how to return multiple function in single module.exports function

My Model Code is as follows:
module.exports = function(){
'use strict';
return {
getAllUsers : getAllUsers,
getWatchlists : getWatchlists,
getUserBidDetails : getUserBidDetails,
addToWatchlist : addToWatchlist,
removeFromWatchlist : removeFromWatchlist,
getUserBuyingLimit : getUserBuyingLimit,
userBidDetails : userBidDetails,
getUserWatchlists : getUserWatchlists
};
}
I have defined all the functions which we are returning in module.exports, but when the last function i.e "getUserWatchlists" get called then it is throwing an error
Error: has no method 'getUserWatchlists'
Might be i am not using the correct way to return multiple function in single module.exports function. Kindly suggest
Why not just set module.exports to the object that you're currently returning? For example:
module.exports = {
getAllUsers: getAllUsers,
getWatchlists: getWatchlists,
getUserBidDetails: getUserBidDetails,
addToWatchlist: addToWatchlist,
removeFromWatchlist: removeFromWatchlist,
getUserBuyingLimit: getUserBuyingLimit,
userBidDetails: userBidDetails,
getUserWatchlists: getUserWatchlists
};
Alternatively if you want to avoid duplicating the names like that, you could just set the functions on the exports object and avoid re-assigning module.exports at all. For example:
exports.getAllUsers = function getAllUsers(...) {
// ...
};
exports.getWatchlists = function getWatchlists(...) {
// ...
};
// ... etc.

Defining modules in UI5

I am trying to keep my code separated in modules. When I defined my first module I extended sap.ui.base.Object and it worked. My question is: Is it a must to extend sap.ui.base.Object when defining my own module? According to the API documentation I tried following example:
sap.ui.define([], function() {
// create a new class
var SomeClass = function();
// add methods to its prototype
SomeClass.prototype.foo = function() {
return "Foo";
}
// return the class as module value
return SomeClass;
});
I required this module inside my Component.js as dependency like this:
sap.ui.define([
"path/to/SomeClass"
], function (SomeClass) {
"use strict";
//var test = new SomeClass();
I always receive a syntax error:
failed to load '[...]/Component.js' from ./Component.js: Error: failed to load '[...]/module/SomeClass.js' from ./module/Service.js: SyntaxError: Unexpected token ;
Does anyone have an idea why this happens? Thanks!
We group code in modules like this for example:
jQuery.sap.declare("our.namespace.iscool.util.Navigation");
our.namespace.iscool.util.Navigation = {
to: function (pageId, context) {
// code here
}
// etc.
}
and call the function of the module like this in a controller
jQuery.sap.require("our.namespace.iscool.util.Navigation");
sap.ui.controller("our.namespace.iscool.Detail", {
// somewhere in this file comes this
handleNavButtonPress: function (evt) {
our.namespace.iscool.util.Navigation.navBackToMaster(
evt.getSource().getBindingContext()
);
},
}
Stupid mistake - missing curly brackets in the docs.
var someclass = function() {} ;

Resources