forEach on an Array - node.js

I'm a little baffled. For some reason this won't work:
[1, 2, 3].forEach(function(num) {
console.log(num)
})
I get this error:
TypeError: Cannot call method 'forEach' of undefined
However, this will:
var nums = [1, 2, 3]
nums.forEach(function(num) {
console.log(num)
})
Anyone have any idea what's going on here?

So, it turns out it was because I'm not using semicolons, and the preceding code conflicted.
var foo = 'bar'
[1, 2, 3].forEach(function(num) {
console.log(num)
})

This is valid in both this jsbin: http://jsbin.com/kawatevovabu/1/edit
and in version v0.10.26 of node.js. Perhaps this is a platform issue?

Related

What to use as inputs in OpenCV calibrateCamera cameraMatrix and distCoeffs parameters?

I'm trying to get calibrateCamera working on my Node.js backend. The library is working fine, but I'm having trouble with the OpenCV functions not giving any error messages if I have faulty inputs. I'm kind of flying in the dark.
But that's beside the point. I've taken 17 images of the chessboard calibration pattern, and got the code to detect the pattern in all of the images. Everything works just fine until I call cv.calibrateCamera(), probably due to me not knowing what I should use as the required inputs for cameraMatrix and distCoeff (4th and 5th input parameters). However, I can't be 100% this is the issue because of not receiving any error messages from errors in the cv... functions.
I tried to follow the example at https://docs.opencv.org/3.1.0/dc/dbb/tutorial_py_calibration.html , but on python in the tutorial you can use None as inputs to cameraMatrix and distCoeff. I tried to use null, but that didn't work either.
Any help would be appreciated.
const size = new cv.Size(9,6);
let mat = null;
let objpt = [];
for(let i=0;i<9;i++) {
for(let j=0;j<6;j++) {
objpt.push(cv.Point(2.5*i,2.5*j,0))
}
}
let objectPoints = [];
let imagePoints =[];
for (let i=0; i < 17;i++) {
mat = cv.imread('./calib/calib'+(i+1)+'.jpg');
let smallmat = mat.resize(756,1008);
const corners = smallmat.findChessboardCorners(size);
if (corners.returnValue) {
objectPoints = objectPoints.concat(objpt);
imagePoints = imagePoints.concat(corners.corners);
}
}
// THIS IS WHERE EXECUTION JUST STOPS WITH NO ERROR MESSAGE
cv.calibrateCamera(
objectPoints,
imagePoints,
new cv.Size(756,1008),
new cv.Mat(3, 3, cv.CV_32FC1,0),
[0,0,0,0,0]
);
According to the test parameters should be passed like this:
[_objectPoints, _objectPoints],
[imagePoints, imagePoints],
imageSize,
_cameraMatrix,
distCoefficients
where
const _cameraMatrix = new cv.Mat([
[800, 0, 100],
[0, 800, 100],
[0, 0, 1]
], cv.CV_64F);
and
const distCoefficients = [0, 0.5, 1.0, 1.0];

zipObject in lodash v.4.x.x

According to the documentation of lodash v3.10.1, it is possible to use _.zipObject such like this:
_.zipObject([['fred', 30], ['barney', 40]]);
// => { 'fred': 30, 'barney': 40 }
but in lodash v.4.15.0 I,m gettin this:
_.zipObject([['fred', 30], ['barney', 40]]);
{ 'fred,30': undefined, 'barney,40': undefined }
I've found a function I need in lodash v4.15.0. This function is fromPairs

Why do I get a wrong keyName in Node.js?

For instance, I send a request like { "arr": [ 1, 2, 3, 4 ] } in front-end.Then I get a request.body in Node.js.But the request.body shows as { "arr[]": [ 1, 2, 3, 4 ] }.I can't find out what the wrong it is.I just receive a wrong keyname in my body's ojbect.How to deal with it?
You are using jQuery.ajax with traditional set to false. It is not "wrong"; some frameworks or languages (notably PHP) expect it that way. If you do not, change the parameter to true.

What is the difference between lodash's _.map and _.pluck?

I have the following code, can anyone tell the difference:
const _ = require('lodash');
const arr = [
{'fname':'Ali', 'lname': 'Yousuf'},
{'fname': 'Uzair', 'lname': 'Ali'},
{'fname': 'Umair', 'lname': 'Khan'}
];
_.map(arr, 'fname');
_.pluck(arr, 'fname');
The output is the same, and both functions are not mutating arr.
In the way you're using them, they basically do the same. That's why .pluck() was removed from Lodash v4.0.0 in favor of using .map() with a string as second argument.
Here's the relevant excerpt from the changelog:
Removed _.pluck in favor of _.map with iteratee shorthand
var objects = [{ 'a': 1 }, { 'a': 2 }];
// in 3.10.1
_.pluck(objects, 'a'); // → [1, 2]
_.map(objects, 'a'); // → [1, 2]
// in 4.0.0
_.map(objects, 'a'); // → [1, 2]

forEach using generators in Node.js

I'm using Koa.js framework and Mongoose.js module.
Normally to get a result from MongoDB I code like this:
var res = yield db.collection.findOne({id: 'my-id-here'}).exec();
But I need to execute this line for every element of an array named 'items'.
items.forEach(function(item) {
var res = yield db.collection.findOne({id: item.id}).exec();
console.log(res) // undefined
});
But this code doesn't run as yield is in the function. If I write this:
items.forEach(function *(item) {
var res = yield db.collection.findOne({id: item.id}).exec();
console.log(res) // undefined
});
I'm not getting the result in res variable either. I tried to use 'generator-foreach' module but that didn't worked like this.
I know that this is my lack of knowledge about the language literacy of Node.js. But can you guys help me finding a way how to do this?
You can yield arrays, so just map your async promises in another map
var fetchedItems = yield items.map((item) => {
return db.collection.findOne({id: item.id});
});
The accepted answer is wrong, there is no need to use a library, an array is already an iterable.
This is an old question, but since it has no correct answer yet and it appears on the first page on google search for the key terms "iterators and forEach" I will respond the question:
There is no need to iterate over an array, since an array already conforms to the iterable API.
inside your generator just use "yield* array" (note the * )
yield* expression is used to delegate to another generator or iterable object
Example:
let arr = [2, 3, 4];
function* g2() {
yield 1;
yield* arr;
yield 5;
}
var iterator = g2();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: 4, done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
For examples and in depth information visit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*
Thanks guys, I've done this using the 'CO' module. Thanks.
var co = require('co');
items.forEach(co(function* (item) {
var img = yield db.collection.findOne({id: item.id}).exec();
}));
EDIT: With the latest version of CO, you need co.wrap() for this to work.

Resources