Is it possible to develop a basic liking feature that relies on stored user IDs within an array?

I am currently working on implementing a like button feature that displays the number of likes based on the length of an array of user ids. However, I am encountering an issue where I cannot seem to save the user id to my mongoDB database. Even though the button increments properly when clicked, the user's id does not get pushed to the "likes" array within the object. Is there a way for me to append the user's id to the "likes" array within the object?

Here is my HTML code:

<button ng-model="object.likes" ng-click="addLike(object)">
    <i class="material-icons">thumb_up</i>
    </br>{{object.likes.length}}
</button>

And this is my JavaScript code:

app.controller('likeCtrl', function($http, $location, etc) {
  $scope.addLike = function(object) {
  $http.put('/objects/' + object.id, {
    likes: $scope.currentUser.id
  }).success(function(object) {
    $scope.object.likes.push(object);
  }).error(function(err) {
  return alert(err.message || "an error occurred");
});
}

Answer №1

When adding an object to the likes array, consider using <code>$scope.object.push(object);
instead of
$scope.object.likes.push(object);
The correct statement will vary based on how the object variable is structured.

Answer №2

After modifying the line from "likes: $scope.currentUser.id" to "likes: {$push: $scope.currentUser.id}", the code successfully executed.

This functionality is a part of deployd, the tool I rely on for creating my api.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is it impossible to nest schemas without using an array?

Can anyone explain why mongoose schema definitions don't allow something like this: var NameSchema = new mongoose.Schema({ first: {type: String, trim: true }, last: {type: String, trim: true } }); var UserSchema = new mongoose.Schema({ name: N ...

Preventing Background Scrolling While Modal is Open in React - Using 'position: fixed' causes the page to unexpectedly scroll back to the top

Currently working on a React website where users can scroll through various posts on the homepage. Each post includes a '...' menu that, when clicked, opens up a modal with additional options. In order to prevent the background from scrolling w ...

What is the best way to locate all mesh faces that are being lit up by a SpotLight

I am working with a THREE.Mesh that consists of a THREE.BufferGeometry containing "position" and "normal" THREE.BufferAttributes. This mesh is being lit by a THREE.SpotLight (which is a cone-shaped light source). Is there a method to ...

What is the best way to transfer slices of a structured Numpy array to a different one?

I am working with two numpy structured arrays named arr1 and arr2. The fields in arr1 include: ['f1','f2','f3']. While the fields in arr2 are: ['f1','f2','f3','f4']. Here is an e ...

Oops! There was a hiccup while trying to run the search query on

When utilizing elasticsearch with angularjs, I implement a search query to determine if a document exists in elasticsearch with specific matching terms. The query then returns a response. However, when attempting to retrieve response.hits.max_score from ...

Utilizing _id for timestamping the date of creation/insertion

If I only require precision up to the number of seconds since the Unix epoch, are there any benefits to retaining a separate creation/insertion date for a document in addition to the _id? (Keep in mind that the _id already contains a timestamp - i.e. numb ...

Obtain an identifier to be used as dynamic data in an ajax request with jQuery

I am struggling to extract the id from links that trigger the same ajax function. I have over 30 links generated dynamically, as shown below: <a href="javascript:void(0)" id="105">Item 105</a> <a href="javascript:void(0)" id="379">Item 3 ...

What could be the reason for the malfunction of Javascript ajax code in IBM Worklight?

I am currently working on developing a hybrid app using IBM Worklight. In order to fetch XML data from the server, I have incorporated the following jQuery code. <script type="text/javascript"> (function ($) { /** ...

Substitute all items identified by a particular tag with a component

Is it possible to replace elements with React? I am interested in replacing all elements with a specific tag with an input field when an event like clicking an 'edit' button occurs. I have experience doing this with jQuery, but I would prefer us ...

Set up MongoDB to exclusively listen on Unix Socket instead of any TCP port

Is there a way to set up mongo DB so that it does not listen on any ports, but only on a Unix domain socket? Given that both my application and the database will be running on the same server, they will communicate solely through Unix domain sockets. In o ...

Encountering issues with verifying login credentials in Angular 2

Greetings! I have designed a login page where the user will be logged in if the response is successful, otherwise an error message will be displayed. Below is the JSON data with email and password fields: { Email": "<a href="/cdn-cgi/l/email-protect ...

JavaScript: protecting data, revealing functionality

Looking to enhance my understanding of Javascript basics in order to delve into client-side frameworks like Knockout and Angular, as well as make headway in learning Node.js. I've selected a simple problem used in teaching C# and am now attempting to ...

Clicking on ajax will disable the submit button and then re-enable it

Hey everyone, I've got this function that's working great but I'm encountering a small problem. $(document).ready(function(){ $("input[type='submit']").attr("disabled", false); $("form").submit(function(){ $("input[type='s ...

Guide to programmatically configuring meta title, description, and image in a Vue.js project with the help of Vue Unhead and Vue Meta

I'm currently developing a Vue.js project where I need to dynamically set the meta title, description, and image based on data fetched from an API. To handle the meta tags, I am utilizing Vue Vue Unhead along with Vue Meta. Below is a snippet of the r ...

The website displays perfectly in Atom, however, it is not functional in any web browser

My website is currently in the development phase, and I'm experiencing issues with certain div elements not positioning correctly once the site goes live. Specifically, the "Follow Us" tab at the bottom of the site, among other divs, has been affecte ...

Aligning a navigation bar with a hamburger menu in the center

I recently implemented a hamburger menu with some cool animations into my site for mobile devices. Now, I am facing the challenge of centering the menu on desktop screens and it's proving to be tricky. The positioning is off, and traditional methods l ...

How do I start using Google Analytics: application.js, ga.js, or a beginner’s guide?

Hello there! I was wondering if anyone could provide a comprehensive guide or step-by-step tutorial on setting up Google Analytics with some examples. The information I found so far only covers signing up and obtaining a tracking code, but it doesn't ...

Creating a Halo (external outline) for a circular sector in THREE.JS

I'm working on adding a halo (external black outline) to certain shapes in three.js. While I was able to achieve this easily with rectangles and circles, I am facing challenges with circular sectors (not full circles). Here is my current attempt: It ...

Is innerHTML incapable of executing JavaScript code?

Experimenting with a new technique where I divide my code into separate files to create multiple HTML pages instead of one large one. Using ajax to load them and then setting the content as innerHTML to a parent div results in clean code that works well in ...

Is it possible to access the Windows certificate store using JavaScript?

Is there a way to access the Windows certificate store using JavaScript? I'm looking to create a web application that can validate user logins by reading their certificates. ...