dec0mpiled
dec0mpiled

Reputation: 99

Mongoose ObjectID not working as parameter in a function?

I am trying to pass a Mongoose ObjectID to a function in an onclick() This is my code (shortened for ease of reading):

<a onclick="Like({{_id}}, {{likes}}, {{dislikes}});" class="btn btn-like"></a>

<p id="{{_id}}"> {{likes}} likes </p>

<script>
    function Like(id, likes, dislikes){
     alert("This function worked! Likes: " + likes);
     document.getElementById(id).innerHTML = likes + 1
    }
</script>

This does not work as expected. When I click the "Like" button, nothing happens. I did inspect the page element for the button, and it shows up as onclick="Like(56ce7a3161714811003ae5f7, 1, 0);". This is what should show up.

As a side note, if I replace {{id}} with a number like 100, or any number, the function does run and alerts correctly. So my question becomes, are you not allowed to have numbers in a function parameter? Or is there a parameter length limit? Thanks for any help!

Upvotes: 2

Views: 402

Answers (1)

Kelvin
Kelvin

Reputation: 690

You need edit: <a onclick="Like('{{_id}}', {{likes}}, {{dislikes}});" class="btn btn-like"></a> Because _id is string, so Like(56e7df....) is different Like("56e7df..."). With Like(56e7df...), browser will compile 56e7df as variable instead of ID string

Upvotes: 3

Related Questions