I'm trying to test this
When I test this code online in runkit.com
*var hash = require('custom-hash');
hash.configure({ charSet: [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z',], maxLength: 26 });
hash.digest('Hello World!');
I get this result:
175GKG3K6UZ6GAEE64BFKUN0MR
I just installed the custom-hash package in Mac and made the file test.js exactly with the code working online and type in terminal node test.js and I get no output
Can you help please?
Thanks in advance!
Replace your final line with something like this:
let result = hash.digest('Hello World!');
console.log('this is the result: ' + result)
Whatever npm.runkit is doing for output you will need to do manually in a node script. console.log is a fine way to do that.
Thanks clay it worked!
After I have achieved the second step, get the hash of a file instead of a string, by modifying the code like this:
const fs = require('fs');
let result = hash.digest(fs.readFileSync('/users/Test.PDF'));
And now, how can I compile it now to be an executable desktop app?
Thanks in advance?
Related
UPDATE: Solved!
The only change needed was making the await of the API call to a return, then the map was returned with both image names and ids. Still trying to get the map converted to an array, but this question for this post is solved. Here's the new printout:
1 face detected from image Family1-Son1.jpg with ID 98091e1e-bc8d-4c93-a850-a115684a6e6e
Family1-Son1.jpg
1 face detected from image Family1-Dad3.jpg with ID f94360f5-feb3-4d14-816f-7d854fc0b34c
Family1-Dad3.jpg
[ { '0': 'F',
'1': 'a',
'2': 'm',
'3': 'i',
'4': 'l',
'5': 'y',
'6': '1',
'7': '-',
'8': 'D',
'9': 'a',
'10': 'd',
'11': '3',
'12': '.',
'13': 'j',
'14': 'p',
'15': 'g',
id: 'f94360f5-feb3-4d14-816f-7d854fc0b34c' },
{ '0': 'F',
'1': 'a',
'2': 'm',
'3': 'i',
'4': 'l',
'5': 'y',
'6': '1',
'7': '-',
'8': 'S',
'9': 'o',
'10': 'n',
'11': '1',
'12': '.',
'13': 'j',
'14': 'p',
'15': 'g',
id: '98091e1e-bc8d-4c93-a850-a115684a6e6e' } ]
[ <2 empty items> ]
[]
I've tried many different methods on here, can't get them to work. This one is the closest I came. I am trying to call an API but on each item in an array. I can't do this in a regular loop, so many reasons why not. So someone said use the array.map() function instead of a loop. I got this far:
const IMAGE_BASE_URL = 'https://csdx.blob.core.windows.net/resources/Face/Images/'
let sourceImageFileNames = ['Family1-Dad3.jpg', 'Family1-Son1.jpg']
// Detect faces in the source image array, then get their IDs
let sourcefaceMap = await Promise.all(sourceImageFileNames.map(async (imageName) => {
// Returns a Promise<DetectedFace[]>
await client.face.detectWithUrl(IMAGE_BASE_URL + imageName)
.then((faces) => {
console.log(`${faces.length} face detected from image ${imageName} with ID ${faces[0].faceId}`)
let id = faces[0].faceId
return { ...imageName, id }
}).catch((err) => {
console.log(`No face detected in: ${sourceImageFileNames[0]}.`)
throw err;
})
}))
let values = Object.values(sourcefaceMap)
console.log(values)
// Create an array to store the source face IDs
var sourceFaceIds = new Array(sourceImageFileNames.length)
console.log(sourceFaceIds)
let ids = sourceFaceIds.filter((id)=> {
return id != null;
})
console.log(ids)
The values seem to be there in when debugging, but then when I try to return the map, it prints out as undefined. Even though the map does do the job of looping to get each id (as seen in the top two print statements).
Here is my printout:
VERIFY
1 face(s) detected from image Family1-Dad3.jpg with ID f94360f5-feb3-4d14-816f-7d854fc0b34c
1 face(s) detected from image Family1-Son1.jpg with ID 98091e1e-bc8d-4c93-a850-a115684a6e6e
[ undefined, undefined ]
[ <2 empty items> ]
[ <2 empty items> ]
Here is a screenshot of the id having value, when I hover over it in the return statement:
Basically, I am trying to do an API call with URL images, then the API will associate an ID with each face in the image. I need the API call to return all those IDs. In this case, it's only 2 IDs. How do I do this? At the end of my code, I just need an array of those IDs. That's all. Thanks.
Your map callback didn't return anything, it only waited. Use
const values = await Promise.all(sourceImageFileNames.map(imageName => {
// now *really* returns a Promise<DetectedFace[]>
return client.face.detectWithUrl(IMAGE_BASE_URL + imageName)
.then(faces => {
console.log(`${faces.length} face detected from image ${imageName} with ID ${faces[0].faceId}`)
let id = faces[0].faceId
return { imageName, id }
}).catch((err) => {
console.log(`No face detected in: ${sourceImageFileNames[0]}.`)
throw err;
})
}));
console.log(values);
I'm also pretty certain that you didn't want to spread the imageName string into an object, and don't need to call Object.values on an array to get its values as an array.
I think the problem you are facing here on line let id = response.faceId.
id is getting its value from response.faceId please do check that faceId property present in the response object. If it is present then code will work as you expected.
I'm trying to use the _.update method inside of the _.forEach method and I'm not understanding why the newly added property has a value of undefined.
FYI, I have to have the function defined separately and then pass it to the _.update method, I can't write it directly into the _.update method (it must be dynamic).
I've tried structuring this in multiple different ways, but none of them work.
let object = [{ 'a': 1, 'b': 1 }, { 'a':1, 'b': 1 }]
function myFunc (row) { return row.a + row.b }
_.forEach(object, row => _.update(row, 'c', myFunc(row)))
console.log(object)
I expected to get:
[{ 'a': 1, 'b': 1, 'c': 2 }, { 'a':1, 'b': 1, 'c': 2 }]
The _.update() method is used for updating an existing property, and it accepts an updater function, and not a value (like the one generated by myFunc).
In your case you should use _.set(), that accepts a value:
const object = [{ 'a': 1, 'b': 1 }, { 'a':1, 'b': 1 }]
function myFunc(row) {
return row.a + row.b
}
_.forEach(object, row => _.set(row, 'c', myFunc(row)))
console.log(object)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And if you don't have to use lodash, you can use Array.forEach():
const object = [{ 'a': 1, 'b': 1 }, { 'a':1, 'b': 1 }]
object.forEach(row => row.c = myFunc(row))
function myFunc(row) {
return row.a + row.b
}
console.log(object)
I have this loop in a Node and Mongo application. console.log will output all the 'product' names correctly, however, rank is usually not set correctly in the database. Interestingly enough, if I put a debug breakpoint in the program and step through slowly, it works. Any idea if there is some sort of race condition taking place, and what the best solution for making this work would be?
async.each(sortedProductsArray, function (product, callback) {
console.log(product.name);
var query = {
productNo: product.productNo
};
var index = sortedProductsArray.indexOf(product)+1;
var update = {
// give it a rank
$set: { 'rank': index }
};
// main products array
products.update(query, update, callback);
});
you should probably be using forEachOf and this will return the index as the second parameter, e.g.:
async.forEachOf(['a', 'b', 'c'], function () {console.log(arguments)});
{ '0': 'a', '1': 0, '2': [Function] }
{ '0': 'b', '1': 1, '2': [Function] }
{ '0': 'c', '1': 2, '2': [Function] }
and then you should be using this index rather than doing another search over your products collection
edit:
when you call products.update(query, update, callback); this affects the products array, and therefore may not be valid when you do your indexOf() (may not exist) which is why the rank sometimes isnt populated
I'm using NodeJS 0.10.13. I'm just curious about the behavior of the following code snippet:
> var a = ['1','2','3']
undefined
> a.map(function(){return path.resolve(arguments[0])})
[ '/Users/user/1',
'/Users/user/2',
'/Users/user/3' ]
> a.map(path.resolve)
TypeError: Arguments to path.resolve must be strings
at exports.resolve (path.js:313:15)
> a.map(path.resolve.bind(path)))
TypeError: Arguments to path.resolve must be strings
at exports.resolve (path.js:313:15)
Why is it that the 2nd and 3rd map calls return an error when the array only has strings? Going to the relevant line in NodeJS's source code yields this:
if (typeof path !== 'string') {
throw new TypeError('Arguments to path.resolve must be strings');
} else if (!path) {
continue;
}
Which makes no sense as to why the arguments are not strings. Does anyone have any clue?
The callback to Array.prototype.map is passed three arguments: current element, index, and the array being traversed.
a.map(path.resolve);
a.map now calls path.resolve using a construct similar to this:
path.resolve.call(undefined, element, index, array);
path.resolve([from ...], to) can accept var args. If you go through the source of path.js
for (var i = arguments.length - 1; i >= -1; i--) {
//..irrelevant lines
var path = arguments[i];
if (typeof path !== 'string') {
throw new TypeError('Arguments to path.resolve must be strings');}
else if (!path) {
continue;
}
}
In the first iteration, path is the third argument, which is an Array.
typeof arrayObject !== 'string' evaluates to true and hence the TypeError
This happens because the parameters passed to each call of the mapped function will get not only the actual elment, but also the array index and the whole array.
To see exactly what parameters gets sent to map, try this:
> var a = ['1', '2', '3'];
['1', '2', '3']
> a.map(function() { return arguments});
[ { '0': '1',
'1': 0,
'2': [ '1', '2', '3' ] },
{ '0': '2',
'1': 1,
'2': [ '1', '2', '3' ] },
{ '0': '3',
'1': 2,
'2': [ '1', '2', '3' ] } ]
Since the object sent to the mapped function (path.resolve in this case) is not a string but an object, you get a TypeError.
http://domain.com/action?params[]=1¶ms[]=2¶ms[]=3
returns:
query: { 'params[]': [ '1', '2', '3' ] }
params[] as name instead of params?
After PHP it's kinda surprise.
jQuery serialization is adding [] on parameters btw.
Are you guys wrote a helper for this or I'm just doing it wrong?
This seems like expected behavior to me; I would be more surprised if the querystring parser removed part of the name. That is, the module is doing exactly what I would expect from a parser which simply splits name/value pairs by '&' and name/value by '=' (and unescapes special characters).
var qs = require('querystring');
qs.parse('params=1¶ms=2¶ms=3'); // Name should be "params"
// => { 'params': ['1', '2', '3'] }
qs.parse('params[]=1¶ms[]=2¶ms[]=3'); // Name should be "params[]"
// => { 'params[]': ['1', '2', '3'] }
This module does parsing as required:
https://github.com/visionmedia/node-querystring
There is another one for complex arrays if this doesn't work:
https://github.com/jazzychad/querystring.node.js
Both found here:
https://github.com/joyent/node/wiki/modules