node.js - Not a string or buffer. Module Crypto -
i created on module util.js function myhash() reuse in different parts of code not working.
error message: this._binding.update(data, encoding); not string or buffer.
app.js
... global.util = require('./util'); global.dateformat = util.dateformat; global.myhash = util.myhash; /***** function *****/ ... app.post('/test', function(req, res){ ... var pass_shasum = myhash('test'); ... util.js
var crypto = require('crypto'); function myhash(msg) { return crypto.createhash('sha256').update(msg).digest('hex'); } exports.util = { ... myhash: myhash(), ... }; any suggestions?
solution:
modify util.js
var crypto = require('crypto'); /* define var */ var myhash = function (msg) { return crypto.createhash('sha256').update(msg).digest('hex'); }; module.exports = { ... myhash: myhash, /* variable not method. @robertklep */ ... };
you shouldn't execute function in exports statement (the msg argument undefined indeed not string or buffer):
exports.util = { ... myhash: myhash, // don't use myhash() ... }; also, when export code this, have require this:
global.util = require('./util').util; (although suggest not using globals).
if don't want .util, export this:
module.exports = { ... myhash : myhash, ... };
Comments
Post a Comment