Can't export multiple modules [ExpressJs] - node.js

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');

Related

Koa cannot get the value of the property inside ctx.request-body

Koa cannot get the value of the property inside ctx.request-body
Project koa is generated by koa-generator
Routing section related code
Either require('koa-body') or require('koa-bodyparser')
console.log("ctx")
console.log(ctx.request.body)
console.log(ctx.request.body.type)
})
The three console.log prints are
ctx
{
account:'root',
password: 'test',
type:0
}
undefined
I can get the object inside the ctx.requisition.body and print it out, but ctx.request.body.type is undefined
How to get 'ctx.requisition.body.account' or 'ctx.requisition.body.password' ?
Maybe if you do this
const myObj = JSON.parse(ctx.request.body)
console.log(myObj.type)
You'll get ctx.request.body.type

module.exports = ({}) vs {}

I work with Koa middleware and in some code I use this code in router:
module.exports = ({router}) => {
//some code
}
if I do:
module.exports = {router} => {
//some code
}
the nodejs app throws an error. So whats the difference between these two different exports except the error part?
{(router)} is just wrong. You should see module.exports as a function so it has () where the arguments go, which in this case is an object {} of the functions you want to export (here router, but it could be multiple just as well.
I think this article gives a very clear explanation: https://www.sitepoint.com/understanding-module-exports-exports-node-js/

Custom helper with handlebars-loader

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

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.

Resources