Why does Ramda work in this example where native Array.map or Lodash do not? - ramda.js

In the process of writing an example for this SO question, this question came up:
Why does the native Array.map throw an error when used like this:
[tmp1, tmp2].map(fs.createReadStream)
.forEach(stream => stream.pipe(jsonStream));
fs.js:1664
throw new TypeError('"options" argument must be a string or an object');
^
TypeError: "options" argument must be a string or an object
at new ReadStream (fs.js:1664:11)
at fs.createReadStream (fs.js:1649:10)
at Array.map (native)
Similarly with lodash… but it works fine with ramda.
// Same error:
_.map([tmp1, tmp2], fs.createReadStream)
.forEach(stream => stream.pipe(jsonStream));
// Works fine:
R.map(fs.createReadStream, [tmp1, tmp2])
.forEach(stream => stream.pipe(jsonStream));
Note, this is the full code from referenced question:
var fs = require('fs');
var path = require('path');
var JSONStream = require('JSONStream');
var tmp1 = path.join(__dirname, 'data', 'tmp1.json');
var tmp2 = path.join(__dirname, 'data', 'tmp2.json');
var jsonStream = JSONStream.parse();
jsonStream.on('data', function (data) {
console.log('---\nFrom which file does this data come from?');
console.log(data);
});
[tmp1, tmp2].map(p => {
return fs.createReadStream(p);
}).forEach(stream => {
stream.pipe(jsonStream);
});
The second argument of fs.createReadStream should be undefined no?

This is most likely due to Array.prototype.map and _.map passing three arguments to the provided mapping function (the value, the index and the collection), while R.map only passes the value.
In your example, fs.createReadStream is being given the array index as its second argument where it expects an options object or string instead, causing the "options" argument must be a string or an object error. If you want to use Array.prototype.map or _.map in this way, you'll need to wrap the method call in a function to prevent the extra arguments:
[tmp1, tmp2].map(p => fs.createReadStream(p))

Related

Vue.js Nuxt - cannot access Array (value evaluated upon first expanding error)

I have the following function which gives me an array called URLs
const storageRef = this.$fire.storage.ref().child(fileName)
try {
const snapshot = storageRef.put(element).then((snapshot) => {
snapshot.ref.getDownloadURL().then((url) => {
urls.push(url)
})
})
console.log('File uploaded.')
} catch (e) {
console.log(e.message)
}
});
console.log(urls)
console.log("about to run enter time with imageurls length " + urls.length)
When I run console.log(URLs) initially I do see the array like the following
[]
0: "testvalue"
length: 1
__proto__: Array(0)
However, there is a small information icon stating
This value was evaluated upon first expanding. The value may have changed since.
Because of this, when I try to get the length of URLs, I get zero, meaning the value is being updated.
Does anyone know what's happening? I am using Vue.JS/Nuxt.

How to pass an array as a parameter to statementExecPromisified(statement,[]) function of sap-hdbext-promisfied in nodejs?

I have an array of users as below
let usersarr = ["'SAC_XSA_HDB_USER_ABC','SAC_XSA_HDB_USER_DEF'"]
I want to fetch data about the above users(if exists) from Hana database. I am using sap-hdbext-promisfied library in node.js.
My database connection is working fine. So, I am trying to execute a select query as below
async function readUsers(xsaDbConn){
try{
let usersarr = ["'SAC_XSA_HDB_USER_ABC','SAC_XSA_HDB_USER_DEF'"]
const checkuserexiststatement = await xsaDbConn.preparePromisified("SELECT USER_NAME FROM USERS WHERE USER_NAME IN (?)")
let checkuserexistresult = await xsaDbConn.statementExecPromisified(checkuserexiststatement, [usersarr])
console.log(checkuserexistresult)
return checkuserexistresult
}catch(err){
console.log(err)
return;
}
}
Below is the output I get
PS C:\Users\Documents\XSA\SAC_POC\cap_njs> npm start
> cap_njs#1.0.0 start C:\Users\Documents\XSA\SAC_POC\cap_njs
> node server.js
myapp is using Node.js version: v12.18.3
myapp listening on port 3000
[]
I get an empty array object as output. This is not the expected output, instead it should provide details about the users as they exist in the database.
The above code works when I provide single user value instead of multiple users in an array as shown below
async function readUsers(xsaDbConn, tempxsahdbusers){
try{
let usersarr = 'SAC_XSA_HDB_USER_ABC'
const checkuserexiststatement = await xsaDbConn.preparePromisified("SELECT USER_NAME FROM USERS WHERE USER_NAME IN (?)")
let checkuserexistresult = await xsaDbConn.statementExecPromisified(checkuserexiststatement, [usersarr])
console.log(checkuserexistresult)
return checkuserexistresult
}catch(err){
console.log(err)
return;
}
}
Output Of Above Code -
PS C:\Users\Documents\XSA\SAC_POC\cap_njs> npm start
> cap_njs#1.0.0 start C:\Users\Documents\XSA\SAC_POC\cap_njs
> node server.js
myapp is using Node.js version: v12.18.3
myapp listening on port 3000
[ 'SAC_XSA_HDB_USER_ABC' ]
So, why is it giving an empty array object when I provide an array as a parameter instead of a variable? Is it possible to provide an array as a parameter to the function statementExecPromisified(statement, []) of sap-hdbext-promisfied library in node.js ?
Your
let usersarr = ["'SAC_XSA_HDB_USER_ABC','SAC_XSA_HDB_USER_DEF'"]
has exactly one value, the String:
"'SAC_XSA_HDB_USER_ABC','SAC_XSA_HDB_USER_DEF'"
When passing the userarr in the statementExecPromisified function as a parameter you are actually passing a nested array in an array. You could either try
xsaDbConn.statementExecPromisified(checkuserexiststatement, [usersarr[0]])
or separate the values in the userarr and add multiple ? in the prepared statement and reference each single value with userarr[x].

How to save List<Point> into preferences and get List<Point> from preferences in flutter?

Error while using json_serializable
json_serializable:json_serializable on .../sign_point_model.dart:
Error running JsonSerializableGenerator
Could not generate fromJson code for valList because of type Point<num>.
None of the provided TypeHelper instances support the defined type.
json_serializable doesn't know how to convert a Point into JSON. Since you know it's just a pair of nums you could easily convert the list yourself.
import 'dart:convert';
void main() async {
var points = [
Point(Offset(123, 456), PointType.tap),
Point(Offset(3.14159, 3.16227), PointType.move),
];
var simplified =
points.map((e) => [e.offset.dx, e.offset.dy, e.type.index]).toList();
String j = json.encode(simplified);
print(j);
var decoded = json.decode(j) as List;
var asPoints = decoded
.map((e) => Point(Offset(e[0], e[1]), PointType.values[e[2]]))
.toList();
print(asPoints);
}

How to correctly get gulp.src globs from a variable?

Given the following Gulp setup
const { src, dest } = require('gulp');
var zip = require('gulp-zip');
var pkgDist = 'packages/';
function pkg(done) {
src(['./**', '!node_modules/**', '!vendor/**', '!.gitignore', '!*.json', '!*.lock'], {base: '..'})
.pipe(zip('archive.zip'))
.pipe(dest(pkgDist))
done();
};
exports.pkg = pkg;
how can I modify it in order to get src globs from a variable, i.e. pkgSrc, something like this:
[...]
var pkgSrc = <what to put here?>;
[...]
src(pkgSrc)
[...]
I've tried to use this var pkgSrc = " ['./**', '!node_modules/**', '!vendor/**', '!.gitignore', '!*.json', '!*.lock'], {base: '..'} "; but it doesn't work.
If it's easier, I'm also open to solutions that result into this src([pkgSrc], {base: '..'})
You can go with just:
var pkgSrc = ['./**', '!node_modules/**', '!vendor/**', '!.gitignore', '!*.json', '!*.lock']
gulp.src first argument can be a string or an array, so now it is an array above.
The second argument is an object of options. Include the options sepearately: {base: '..'} so
src(pkgSrc, {base: '..'})

Getting error for variable does not exist in current context cshtml?

i have following code for getting error
$('#Template').click(function () {
var selectedTempleteType = $('#BulkLoadActionDropDownId option:selected').val();
var path = '#Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
$(this).attr("href", path);
});
error showing for "selectedTempleteType".
selectedTempleteType is a client-side JS variable, you cannot use it inside #Url.Content() which runs server-side (and #Url.Content() is incorrect to map URL path with query string, use #Url.Action() instead). You should change from this:
var path = '#Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
to this one:
var path = '#Url.Action("DownloadBulkLoadActionTemplate", "Upload")?templateType=' + selectedTempleteType;
Or using placeholder inside #Url.Action() with replace() in client-side:
var path = '#Url.Action("DownloadBulkLoadActionTemplate", "Upload", new { templateType = "xxxx" })';
path = path.replace("xxxx", selectedTempleteType);