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
Louis Law
@louiscklaw

2021-10-17_14-56.png
May I know the "name" of these question marks?
Please leave me the name/keywords of this is good enough because it is hard to google for this symbol for me. i can do the rest study/read docs/test myself

thanks in advance...

Salathiel Genèse
@SalathielGenese
Optional chaining, @louiscklaw
2 replies
(on the right)
Just a normal string
(on the left)
Jordan Harband
@ljharb
@louiscklaw please don't cross-post; i answered you the same in the airbnb/javascript gitter (this was the best place to ask tho)
node.js.developers.kh
@nodejsdeveloperskh

I am working with node-mongodb-drive and i can not use $merge stage in the aggregate. mongodb throw this error: MongoError: Unrecognized pipeline stage name: '$merge'.

what can i do?

  • the same aggregate works in the mongo-compass.
Zerka1982
@Zerka1982
@Zerka1982
Hi guys !
ANybody here to support me on one task
If user inputs an email for example@gmail.com , I would like to force the user not to add any extra letters after the .com
is there a way to do this please?
Salathiel Genèse
@SalathielGenese

@Zerka1982 , consider:

  1. Input pattern attribute (HTML)
  2. DOM form field custom validity + regular expression (JavaScript)
  3. Or a combination of what precedes

NOTEWORTHY

I hope you have strong reasons to enforce such rules. Some emerging TLD and thus, email addresses may well have more that three letters at the end (as the spec suggests)... Which is a concern I have from time to time with online service rejecting .berlin, .name, even sometimes .cloud

Anyabuike Kingsley
@kingsleykbc

What is the best way to represent related data in an API response object?

E.g., Say I have an order object. What method would better represent it (relating to logic and performance)?

Prefixing the keys:

{
    "quantity": 2,
    "date": 237823872,
    "productID": 1
    "productName": "iPhone",
    "productSlug": "www.site.com/iphone",
    "sellerID": 2,
    "sellerName": "Apple",
    "sellerEmail": "apple.com"
    "price": 500.00,
    "paymentStatus": "Successful"
}

or nested:

{
    "quantity": 2,
    "date": 237823872,
    "product": {
        "id": 1
        "name": "iPhone",
        "slug": "www.site.com/iphone",
    },
    "seller": {
        "id": 2,
        "name": "Apple",
        "email": "apple.com"
    },
    "payment":{
        "price": 500.00,
        "status": "Successful"
    }
}
Marco Vinicio
@Mark-B-88
@kingsleykbc As far as logic, nested is much better because it's more detailed. So if you're working in a team, or you need someone to help you with your code, the nested approach makes more sense. As far as performance, that's something I've not entirely sure about.
Andres Mazzo
@andresmazzo
@kingsleykbc i agree with @Mark-B-88 . Performance could be a little more on nested objects due to serialization of json, which increase cpu load. But it only applies for high traffic and big objects to see a significant variation
Anyabuike Kingsley
@kingsleykbc
@Mark-B-88 , @andresmazzo Thanks a lot
Phuong Nguyen
@neunygph
this is the most active channel I have seen
Salathiel Genèse
@SalathielGenese
NodeJS 17.0.1 error (latest, Linux, x64)
$ node -v
bash: /home/salathiel/.local/bin/node/bin/node: cannot execute binary file: Exec format error
Jordan Harband
@ljharb
why are you installing node with something that's not nvm
Salathiel Genèse
@SalathielGenese
The habit, I guess - tar -xf archive -C target/
I just tested with v17.0.0 and no problem with that
Jordan Harband
@ljharb
tbf i don't have linux, but nvm install 17 worked fine on 17.0.1
Salathiel Genèse
@SalathielGenese
okay
zillerium
@zillerium
I want to setup class vars in nodejs, like you have in Java etc. I am seeing only the constructor, here is a code example -
class myclass {
    var carMake = 'Ford'

}
Jordan Harband
@ljharb
@zillerium a) do not strive to make anything like java, that's always a downgrade. b) class in JS is not really classes, it's good sugar for making a constructor function and a prototype object. a "class var" is probably meant as a static property on the constructor function.
you can't declare variables inside a class body, it's not a scope.
so you can do class X { static make = 'Ford'; } if you want
zillerium
@zillerium
@ljharb thanks
Jordan Harband
@ljharb
that's public tho
zillerium
@zillerium
what is public?
Jordan Harband
@ljharb
you can do class X { static #make = 'Ford'; } for private.
"public" is "reachable" - things on the outside can see and change it
"private" is "unreachable" - and unlike every other language, private in JS actually means private; there's no reflection mechanism whatsoever
zillerium
@zillerium
I was about to edit my code snippet with a method or func to update carMake, eg setCarMake(carMake){ this.carMake = carMake; // but not in a constructor}
Jordan Harband
@ljharb
and there's no "protected" because that concept doesn't make any sense in JS; things are either reachable or not
sure, you can do that in either case. but it makes more sense to have it be private if you want to provide a setter function
that way nobody can set it a different way
however, classes (like for most things) are not a great way to model cars, since they aren't an inheritance tree. they're more like a collection of traits and metadata.
zillerium
@zillerium
cars are just an example, I am not actually working with cars, but I did see a lot of car models in OO systems
Jordan Harband
@ljharb
sure, but most of those are bad examples that only really exist in university :-)
zillerium
@zillerium
to summarise, I can instantiate the class and set variables via the constructor but I cannot have just variables in the class which are modified by functions (eg setters and getters)?
Jordan Harband
@ljharb
you can have properties, but not variables
variables can only exist in a scope, and a class body isn't one
zillerium
@zillerium
About cars, there were commercial examples.
ekranoplan
@ekranoplan:matrix.org
[m]

to summarise, I can instantiate the class and set variables via the constructor but I cannot have just variables in the class which are modified by functions (eg setters and getters)?

I think this would accomplish what you're trying to do:

class Car {
  #fuel = 0;
  #make;

  constructor(make) {
    this.#make = make;
  }

  get make() { return this.#make; }

  addFuel(quantity) {
    this.#fuel += quantity;
  }
}

In this case, #fuel is a private "variable" that can be modified within functions (e.g. the addFuel method). Technically #make is also a variable but I have easily written it in such a way that you cannot modify the make after the car has been constructed (since a Corolla will always be a Toyota) - there is only a getter for the make and no setter, and the #make field is private so it's not exposed to clients.

Zerounodue
@Zerounodue
@zillerium if you come from java you might like Typescript :) have a look https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-oop.html
zillerium
@zillerium
Is there a way to use async/await when a nodejs api server starts up (toload some vars from a file, eg json). I get an error when I tried and just place that at the start of the program because it says I cannot use await outside of an async function. My aim is only to load some variables once only (the json objects are very large and cannot be just coded into the source code and also they could change.
Stephen James
@sjames1958gm
Top level await is available in node 14
Jordan Harband
@ljharb
only in native ESM.
(which should generally be avoided)
1 reply