Hello all, completely stuck trying to use the 'async' module and making a waterfall of 3 functions. The first of the 3 functions is an async function downloads(), returning a promise in the format [{ treatments, profiles, entries}]. The other 2 functions main() and upload() are sync.
These complex functions are defined alsewhere and called at the top of the JS file (main, downloads, upload). Also, NS are parameters { key1 : 'value1', key2 : 'value2'} declared at the top..
This is a syntax that works, and which I am trying to write as a waterfall :
const test = async () => {
const params = { ISF: 2, CR: 10, DIA: 6, WEIGHT: 90, TP: 75, CARBS_ABS_TIME: 360 };
return downloads(NS)
.then(({ treatments, entries }) => {
const newEntry = main({ entries, treatments, env: params });
console.log(newEntry);
//return newEntry;
upload(newEntry, NS);
});
};
test();
... and this is my non functioning waterfall:
const async = require(`async`);
async.waterfall([
async function myFirstFunction(callback) {
const down = await downloads(NS);
const treatments = down.treatments;
const entries = down.entries;
console.log(`treatments:`, treatments, `entries:`, entries);
callback(null, treatments, entries);
},
function mySecondFunction(treatments, entries, callback) {
const newEntry = main({ entries, treatments, env: params });
callback(null, newEntry);
},
function myLastFunction(newEntry, callback) {
const upup = upload(newEntry, NS);
callback(null, upup);
},
], function(err, result) {
console.log(result);
});
When the first function runs, the treatments etc... are output to the console, but after that I just get "undefined". Apparently mySecondFunction either doesn't get the callback from the first, since newEntry isn't created ?
Help very appreciated.
const async = require( 'async' );
function f1( callback ) {
console.log( 'f1' );
callback( null, 1 );
}
function f2( number, callback ) {
console.log( 'f2 - ', number );
callback( null, number, 5 );
}
function f3( n1, n2, callback ) {
console.log( 'f3 - ', n1, n2 );
callback( null, n1, n2, 10 );
}
async.waterfall([f1, f2, f3], function finalCallback( err, a, b, c) {
console.log( a, b, c );
});
upload
return? a promise? I don't think a promise is something you should pass to the callback
@sjames1958gm @jakubiszon Thanks, that was it. The async function inside the waterfall => no callback !
Here's the working solution. Also, the last function is now better formulated to display errors.
const async = require(`async`);
async.waterfall([
async function myFirstFunction() {
const down = await downloads(NS);
return down;
},
function mySecondFunction(down, callback) {
const treatments = down.treatments;
const entries = down.entries;
const newEntry = main({ entries, treatments, env: params });
callback(null, newEntry);
},
function myLastFunction(newEntry, callback) {
const upup = upload(newEntry, NS);
callback(null, upup);
},
], (err, result) => {
if (err) {
// Displaying error
console.log(err);
}
else {
// Displaying result
console.log(result);
}
})
Now that this part is done, the next tricky part is is running this waterfall on each element of a mongoose query. I understand async mongoose queries return thenables. If my query returns 10 "users", the format of the returned data is that of an array. How would I write this ?
const myQuery = Users.find( { onOffSwitch: `on`, nsUrl : {$ne : ``} } );
return myQuery.then(
function(value) { /*run a forEach loop executing the async.waterfall() */ },
function(error) { /* log the error */ }
)
);
I know this is too difficult for me, but you eat an elephant one bite at a time...
const myQuery = Users.find( { onOffSwitch: `on`, nsUrl : {$ne : ``} } );
return myQuery
.then((resultArray) => {
//console.log(`resultArray: `, resultArray);
const numberOfUsers = Object.keys(resultArray).length;
for (let i = 0; i < numberOfUsers; i++) {
// loop stuff on the resultArray...
<div class="gift-up-target" data-site-id="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" data-platform="Other"></div>
<script type="text/javascript">
(function(g, i, f, t, u, p, s) {
g[u] = g[u] || function() {
(g[u].q = g[u].q || []).push(arguments)
};
p = i.createElement(f);
p.async = 1;
p.src = t;
s = i.getElementsByTagName(f)[0];
s.parentNode.insertBefore(p, s);
})(window, document, "script", "https://cdn.giftup.app/dist/gift-up.js", "giftup");
</script>
Deployment issue with github pages:
after commit all the changes pushed to github remote repo, then
added the url of homepage https://<username>.github.io/<app-name>
in package.json file and
also added this to script
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
then runnpm run deploy
but the deployed link showing nothing blank page(
When it didn't work I tried this..
homepage: https://<username>.github.io/
then
"predeploy": "npm run build",
"deploy": "gh-pages -b main -d build",
and then again deployed but this time I have another new issue..
in github something pushed annoying,
and these are my actual files which I pushed..
npm test
. It has several aliases that call variations for different environments. Ordinarily, I would attempt to use inspect
and call the file directly or insert it in the alias. This doesn’t seem possible due to nested alias calls. Is there an easier way to call Node’s inspector maybe using an environment variable or doesn’t npm have an easier way?
Hello everyone, I am trying to convert some code using the deprecated external "crypto" package into a new version using the nodeJS integrated "crypto".
The old one using SHA1 encryption would be :
const crypto = require(`crypto`);
const { createHash } = require(`crypto`);
const hash = createHash(`sha1`);
hash.update(api_key);
const hash_secret = hash.digest(`hex`);
console.log(`this is the hashed api_key:`, hash_secret);
The integrated "crypto" is more complex, here is an example using AES-256-CTR and a 32 bytes key :
const crypto = require(`crypto`);
const algorithm = 'aes-256-ctr';
const secretKey = `vOVH6sdmpNWjRRIqCc7rdxs01lwHzfr3`;
const encrypt = (text) => {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return {
iv: iv.toString(`hex`),
content: encrypted.toString(`hex`)
};
};
Now I need to use this "new" crypto module to obtain the same result as I got with the old code. Apparently the algorithm can be specified as :
const algorithm = `aes-256-cbc-hmac-sha1`; // instead of 'aes-256-ctr'
... but still I need some 32 byte key and moreover I get an error :
node:internal/crypto/cipher:180
const ret = this[kHandle].update(data, inputEncoding);
^
Error: Trying to add data in unsupported state
at Cipheriv.update (node:internal/crypto/cipher:180:29)
etc...
Any idea how I could obtain the same result I got earlier with the deprecated solution ?