Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jun 02 2020 06:16
    @ljharb banned @frank-dspeed
  • Apr 27 2018 01:23
    @MylesBorins banned @fjeddy
  • Feb 24 2016 08:26
    @rvagg banned @jonathanenbe
Jordan Harband
@ljharb
most people, i'd expect.
blocking things is pretty stupid when everyone's got an unblocked phone in their pocket
lucifer
@timmonscharlie413
yah
im tryna make a app but i need a node's code for one of the features
Lorenzo Sandini
@lsandini

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.

Stephen James
@sjames1958gm
Don’t your functions have to return something to get passed to the next function.
Not sure why you have callback in each function. Looking at the docs callback is only for after the last function is called.
Oh I see didn’t look at the example hmm it does have form that you are using.
https://caolan.github.io/async/v3/global.html
I did find this where it talks about when one of the functions is marked async.
Lorenzo Sandini
@lsandini
That waterfall thing is all new to me, I got the idea from here
K Dhanesh
@dhanupreeth_twitter
Hey all, can anyone help to integrate the hubot with google chat. It’s not straight forward like hubot + slack.
Jakub Z
@jakubiszon
@lsandini maybe first try something simpler first? if I remember correctly I had some dificulties when mixing promises and callbacks in the async module
this seens to work fine:
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 );
});
also - what does the call to upload return? a promise? I don't think a promise is something you should pass to the callback
Lorenzo Sandini
@lsandini

@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);
        }
    })
Lorenzo Sandini
@lsandini

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...

Lorenzo Sandini
@lsandini
Oh nevermind. I got it !
        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...
lucifer
@timmonscharlie413
what are u making?
K Dhanesh
@dhanupreeth_twitter
Hey All, need help to integrate the hubot with google chat.
Lorenzo Sandini
@lsandini
@timmonscharlie413 me ? Moving my standalone CGMSIM from a single user app to a (free) SaaS on heroku.
lucifer
@timmonscharlie413
ohh nicee
Tânia P
@ButeoButeo
Hi, got timeout in context.waitForEvent("page"), in POM Playwright . Any Tip?
svimanet
@svimanet:matrix.rongepaadl.com
[m]
increase timeout threshold? :v
await promise?
(just rubbeducking)
lucifer
@timmonscharlie413
anyone wanna help me code?
Jack Devlin
@jackdevlin23
<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>
Im trying to imbed GiftUP on my angular app
But nothing shows when I load the page
Should I place the above code directly into the html file / should it be surrounded by anything
Any help greatly appreciated
ledlamp
@ledlamp:matrix.org
[m]
hi i like nodejs
Lorenzo Sandini
@lsandini
Hello all, here’s a little thing I have never done before. I need users to declare a certain value in a form field, but this value can be declared in either mmol/l or mg/dl units. I’ll add a radio button selecting the unit. Once the form is submitted, the value must be stored in a mongo collection as mg/dl. I guess the transformation should be done server-side, as the value and the unit are received by the API, before being sent to the db ? Or is there a wait to do it the form before or as it is being sent ?
Noob question, I know ☺️
ekranoplan
@ekranoplan:matrix.org
[m]
Given these particular constraints it does not matter whether where the value is converted. Clients always have complete flexibility to send whatever parameters they want but even if we assume that a prankster edits the JS code running on your page, he's only messing up his own measurement.
Conventionally, conversion is done on the server side. This is because the user might have a preference for the units they want to see on other pages, and this would be stored in your database. You could then use some sort of "middleware" for units that automatically converts numerical values based on the user's preference. It also lets users access your website using devices that don't support JS.
Lorenzo Sandini
@lsandini
Thanks, I think I did it correctly then 👍🏼
Fran
@Paker30
Hi guys, I'm doing a small app with pouchDB and I was wondering if you might now any module to sync pouchDB trough google drive, dropbox or similar :)
lucifer
@timmonscharlie413
hello
Suchitra Giri
@SUCHITRAGIRI
Screenshot (1).png
Screenshot.png

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 run
npm 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..

javaexpt
@javaexpt

Working:
xhr.open('POST', "employee.jsp?empId=123", true);
xhr.send();

Not-working:
xhr.send(params); where params is 'empId=123'
Not sure what is the different between the two.

Can someone please explain.

Mike
@_mikeusa_twitter
@javaexpt
  1. Ew xhr :)
  2. Did you set your request headers? xhr.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’)
  3. Consider using application/json and passing a JSON string.
javaexpt
@javaexpt
@_mikeusa_twitter i have set the header but the browser or ended with javascript error so removed the header or content-type. is the above header is mandatory or required?
frank-dspeed
@frank-dspeed:matrix.org
[m]
Little update from me starting tc39 proposal for importScripts() to get integrated directly into the ECMAScript Standard will post link soon if some one wants to join that effort
Kontributer
@Kontributer
I’m trying to debug a repo with 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?
frank-dspeed
@frank-dspeed:matrix.org
[m]
i am not sure what your talking about @Kontributer what has npm to do with environment variables and inspect?
5 replies
frank-dspeed
@frank-dspeed:matrix.org
[m]
the complet spec is as following: workerGlobal.importScripts(...urls)✔MDN
Fetches each URL in urls, executes them one-by-one in the order they are passed, and then returns (or throws if something went amiss).
Lorenzo Sandini
@lsandini

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 ?

Lorenzo Sandini
@lsandini
Duh, looks like I don't need all that cipher stuff. Should have read the docs first, sorry.