These are chat archives for FreeCodeCamp/HelpJavaScript

14th
Feb 2018
Nate Mallison
@NJM8
Feb 14 03:10
@JLuboff Are you using body parser?
mohib98
@mohib98
Feb 14 03:26

I have a task that I couldn’t complete

textarea where users can enter a text. Then there is a button used to count the number of each word and display the result alphabetically and divided into which letter the word.
here's where i have come so far, anyone who can help?

<body>
<p><b> write texts inside the textarea to count every single word</b></p>
<p>
<textarea id = "input_21" rows="10" cols="100"></textarea><br/>
<button id="button">counter</button>
</p>
<p id="demo"> </p>


<script>

var Words = [""];
var demo = [""];

var button = document.getElementById("button");

button.addEventListener("click", splitWords);

function splitWords() {
demo = document.getElementById("input_21").value.split(/[\n <>.,\?]/);
console.log(demo);
}

</script>
</body>

aRtoo
@artoodeeto
Feb 14 04:32
hey guys im getting the dom element but im getting undefined when i specify the index. so weird. but on the others im getting the result.
   const submitBtn = document.getElementsByTagName('button');
    console.log(submitBtn[0]) // returns undefined
    const closeBtn = document.getElementsByClassName('fa-times');
    console.log(closeBtn[0]); // returns undefined
  const submitBtn = document.getElementsByTagName('button');
console.log(submitBtn) // returns an array
const closeBtn = document.getElementsByClassName('fa-times');
console.log(closeBtn); // returns an array
aRtoo
@artoodeeto
Feb 14 04:38
@mohib98 hey bro you could do STRINGHERE.split(' ').map() => { 'len Of words:' + STR.len }.sort(); you could chain it in one function havent tried it but the logic is the same. you want to count the words. split it, then count letter right? then sort them. abiut the divide i really didnt understand. sorry bro. thats all
Aniculaesei
@Aniculaesei
Feb 14 06:11
Hi guys.I've installed an extension for Visual Studio Code for autocomplete.But it doesnt work....How I can activate it?Thank you
iso
@iso1048
Feb 14 06:11

@artoodeeto bro I put the following at the bottom of your js code, and it worked as expected.

setTimeout(function(){
  const submitBtn = document.getElementsByTagName('button');
    console.log(submitBtn[0]);
    const closeBtn = document.getElementsByClassName('fa-times');
    console.log(closeBtn);
},2000);

I guess that you were trying to log to the console elements that did not exist, because the button element is created later, but the elements still show in the HTML collection because 'An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.' (mdn)

aRtoo
@artoodeeto
Feb 14 06:15
@user-ap yup bro. go it. thank you so much
CamperBot
@camperbot
Feb 14 06:15
artoodeeto sends brownie points to @user-ap :sparkles: :thumbsup: :sparkles:
:cookie: 484 | @user-ap |http://www.freecodecamp.org/user-ap
Tiago Correia
@tiagocorreiaalmeida
Feb 14 10:14
gm everyone
Stephen James
@sjames1958gm
Feb 14 11:03
@tiagocorreiaalmeida :coffee:
Tiago Correia
@tiagocorreiaalmeida
Feb 14 11:14
all good?
Stephen James
@sjames1958gm
Feb 14 11:17
So far so good :)
Tiago Correia
@tiagocorreiaalmeida
Feb 14 11:23
good to hear :D
FlashHero
@FlashHero
Feb 14 12:21
What's a good project to practice nodejs, reactjs, redux and mongodb
Tiago Correia
@tiagocorreiaalmeida
Feb 14 12:22
the fcc projects
the book's one
or the poll's
Muhammad Hasham
@MohammadHasham
Feb 14 15:42
any mongo expert?
Tiago Correia
@tiagocorreiaalmeida
Feb 14 15:45
no experts just people used to it a bit, @MohammadHasham in what cna I help?
Muhammad Hasham
@MohammadHasham
Feb 14 15:46
@tiagocorreiaalmeida
image.png
can you help me with this
Tiago Correia
@tiagocorreiaalmeida
Feb 14 15:47
if im not mistakenh you are doing a .find query right?
you are passing as an object id a number 2, that's not a valid object id
Muhammad Hasham
@MohammadHasham
Feb 14 15:48
yeah i'll show you
just a sec
  User.find({"_id":req.user.id},(err,result)=>{
      result[0].polls.push({ _id:"2",name: req.body.title,options: []});
      result.save();
    });
@tiagocorreiaalmeida you are exactly right
can you point that where i may be wrong?
Tiago Correia
@tiagocorreiaalmeida
Feb 14 15:52
can you console.log the req.user.id?
cn you show me how your users collection looks like?
Muhammad Hasham
@MohammadHasham
Feb 14 15:53
const mongoose = require("mongoose");
const {Schema} = mongoose;
const polls = require('./Polls');
const User = new mongoose.Schema({
  twitterId: String,
  polls:[{
    type: Schema.Types.ObjectId,
    ref: 'polls'
  }]
});

mongoose.model("users",User);
@tiagocorreiaalmeida
here is the Users collection
req.user.id works fine actually
@tiagocorreiaalmeida
Tiago Correia
@tiagocorreiaalmeida
Feb 14 15:59
_id its an object id
when yoyu try to set a number or any oither string it will trhow an error
those are not like the sql's id that you can increment those are generated by mongo
Muhammad Hasham
@MohammadHasham
Feb 14 15:59
so what should i do then?
Tiago Correia
@tiagocorreiaalmeida
Feb 14 15:59
what are you trying to achieve?
Muhammad Hasham
@MohammadHasham
Feb 14 16:00
image.png
just trying to get rid of this error @tiagocorreiaalmeida
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:03
dont inject the id?
are you trying to update the one you founded?
like
User.find({"_id":req.user.id},(err,result)=>{
you found this regist and you wanna update it?
ok nm I got your id
change it from _id to id
since will be your personal numbering
and will work
Muhammad Hasham
@MohammadHasham
Feb 14 16:04
okay sure
let me try
image.png
@tiagocorreiaalmeida
still the same problem
can you please have a look here
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:10
can I see your code?
can you console the req.user.id?
Jason Luboff
@JLuboff
Feb 14 16:11
Morning
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:11
    User.find({"polls._id":"2"},(err,result)=>{
      cons
as I told you
the number 2 or the string 2
aint a object id
Jason Luboff
@JLuboff
Feb 14 16:11
@MohammadHasham You need to use ObjectId for your id's it looks like based on that console screen
Muhammad Hasham
@MohammadHasham
Feb 14 16:12
@tiagocorreiaalmeida @JLuboff should i do something like this
id: ObjectId
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:12
no
object id are generated
by the mong itself
Jason Luboff
@JLuboff
Feb 14 16:12
_id: ObjectId(whateverTheIDIs)
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:12
you dont need to generate them
Jason Luboff
@JLuboff
Feb 14 16:13
you need to include const ObjectId = require('mongodb').ObjectId
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:13
I think he just wanna create a poll right?
Muhammad Hasham
@MohammadHasham
Feb 14 16:13
@JLuboff this is while creating right?
@tiagocorreiaalmeida yes actually
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:13
you dont need to set the _id
by yourself
mongo does it
Markus Kiili
@Masd925
Feb 14 16:14
:wave:
Jason Luboff
@JLuboff
Feb 14 16:14
Ya, preferably you let Mongo create the ID's as to avoid duplicate ID's
Muhammad Hasham
@MohammadHasham
Feb 14 16:15
okay so should i remove that field in my schema and while inserting as well
Tiago Correia
@tiagocorreiaalmeida
Feb 14 16:17
hey @Masd925
@MohammadHasham indeed
I dont know if you do know about it
but there's a thing calleds robo mongo google it
will make easier to see the data if you are new to this
Jason Luboff
@JLuboff
Feb 14 16:18
@tiagocorreiaalmeida Psh.. be a man about it! Use the mongo shell
Muhammad Hasham
@MohammadHasham
Feb 14 16:20
actually i am using mlab.com
so the db is not on my own pc
Jason Luboff
@JLuboff
Feb 14 16:20
That doesn't matter
You can use robo mongo or the mongo shell regardless
Muhammad Hasham
@MohammadHasham
Feb 14 16:28
image.png
@JLuboff is this right that i am getting just one collection in robomongo?
Jason Luboff
@JLuboff
Feb 14 16:29
I don't know your structure, but if you only have one collection then ya. But that looks like a document, not a collection
Muhammad Hasham
@MohammadHasham
Feb 14 16:30
am i doing right?
Nerando Johnson
@Nerajno
Feb 14 16:38
greetings all ... I am trying to append data to an accordion that that toggles showing a card .... doing the twitch viewer
i am kinda lost
any suggestions
Stephen James
@sjames1958gm
Feb 14 16:48
@Nerajno Do you have code that doesn't work?
Nerando Johnson
@Nerajno
Feb 14 16:57
i do but
        $(".tab-main-content").append('<div id=' + convertedUserName + '><button class="accordion" data-toggle="collapse" data target=' + convertedUserName + '>' + '<i class="fa fa-window-close" aria-hidden="true"></i>' + ' ' + convertedUserName +
          ' is offline.' + '</button><panel id="testing" class=""><div class="container py-3"><div class="card"><div class="row"><div class="col-md-4"><img src="https://farm5.staticflickr.com/4675/24807227997_48796d28ea.jpg"></div><div class="col-md-8"><div class="card-block px-3">' +
          '<p claass=' + gameName + '> Game Name: ' + gameName + '</p><p class=' + status + '> Current Game Status : ' + status + '</p><p class=' + viewers + '> Live Viewers : ' + viewers + '</p><p class=' + channelAddress + '><a target="_blank" href=' + channelAddress +
          '> Channel Web Address : ' + channelAddress + ' ' + '<i class="fa fa-external-link" aria-hidden="true"></i></a></p></div></div></div></div></div></panel></div>');
@sjames1958gm i copied some of my code but i understand it
Stephen James
@sjames1958gm
Feb 14 17:13
@Nerajno I see claass is mispelled - also you need to work on more readable code.
Nerando Johnson
@Nerajno
Feb 14 17:15
ok
Jason Luboff
@JLuboff
Feb 14 17:58
@MohammadHasham I'm not too familar with using mongoose and models, but I don't think you want to be defining _id in your schema. Let mongo assign that itself
Matej Bošnjak
@mbosnjak01
Feb 14 21:25
Supp. Did anyone use Youtube API?
Gersho
@Gersho
Feb 14 21:27
i have
you mean the one to search info about a video ? or the player itself