Expanding mongoDB database by utilizing a Java endpoint using JavaScript

It would be quite impressive if you could provide an answer to my current dilemma. Currently, I am developing a full stack web application that comprises of three main components:

  1. A collection of Articles stored in MongoDB
  2. A backend developed in Java 8
  3. A front end implemented in JavaScript

The functionality of the program should follow this sequence:

  1. The user accesses the webpage and enters text into a form specifying the title, authors, genre, and content.
  2. Upon clicking the "addArticle" button, the data should be serialized into raw JSON and sent to the addArticle endpoint.
  3. The data is then sanitized and submitted to the database as an article.

I am encountering multiple issues, which are as follows:

  1. Despite having

    $(document).on("submit", "#add-Article", function(e) {e.preventDefault();}
    in place, JavaScript keeps refreshing the page when I click the submit button.

  2. It seems like the endpoint is not being hit at all.

  3. No data is being saved in the database.

What I know works and what I have attempted:

  1. I have successfully tested adding to the "articles" collection through Postman.

  2. I have confirmed that JavaScript is connected by placing an alert at the beginning of main.js.

  3. After extensive research, I have discovered that I can use plain JavaScript to perform tasks like "articles.save" on the front end. However, I want the data to be processed through Java for sanitation and ObjectIds incrementation.

Relevant code snippets:

Form

<form id="add-article">
<input type="text" id="title"/>
<label for="title">Title </label>
<input type="text" id="authors"/>
<label for="authors">Author(s) </label>
<input type="text" id="genre"/>
<label for="genre">Genre </label>
<input type="text" id="content"/>
<label for="content">Content </label>
<button id="addArticle">Submit Article for Peer Review</button>
</form>

JavaScript

alert('JS is linked to page!');

// JavaScript code block

Controller Endpoint

`@RestController

@RequestMapping("/articles") public class ArticleController { // Java code block

Having struggled with this issue for more than a day, any assistance would be greatly appreciated. I suspect there might be an issue with the JavaScript implementation.

Answer №1

Dear user, please double-check your jQuery selector as it seems to be incorrect (it should be "add-article" instead of "add-rticle"). Remember that jQuery selectors are case sensitive.

    $(document).on("submit", "#add-article", function(e) {
    e.preventDefault();
        alert('submit Activated');
    var title, authors, genre, content;
    title = $("#title").val();
    director = $("#authors").val();
    rating = $("#genre").val();
    notes = $("#content").val();
    var myArticle = new Article(title, authors, genre, content);
    alert(myArticle.title);
    addArticle(myArticle);
    //$("#add-article")[0].reset();
    $("#title").focus();

});

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

Utilizing Regex to Retrieve a Specific Value from a String in Java

Given a string containing multiple pairs in the format letter#number, I am looking to create a program that can extract a specific pair based on the input letter. For example, if the input is a, the program should return a#1. If the input is b, it should ...

Instructions on creating a Superfish menu with a vertical layout in the first level and a horizontal layout in the second level

Currently, I am using the Superfish menu in Drupal7 and have designed my first item level to be vertical. However, I now want to style my second item level horizontally. I have tried various CSS approaches and added some class names via jQuery like $(&apo ...

Problem with Handlebars: When compiling, TypeError occurs because inverse is not recognized as a function

I've hit a roadblock with an error that I just can't seem to solve. My goal is to dynamically compile a template extracted from a div on the page, provide it with content, and then display it in a designated area of my webpage. Here's the s ...

Reply in Loopback on remote method beforeRemote hook is to be sent

In my application, I am utilizing loopback v3. The specific use case I am tackling involves validating the presence of a token in the request header. If the token is invalid, I need to send an appropriate message in a standardized format. My approach has b ...

Ways to properly update a state variable when using onClick within a functional component

I'm having an issue with my tabs that are supposed to display different products and filters when clicked. Currently, I have to click each tab twice before the 'options' list updates with the correct filters. I know that calling setOptions w ...

Sending cookies via POST/GET request with Credentials is not functioning

Greetings, despite the numerous duplicates of this inquiry, my attempt to solve it has been unsuccessful. Therefore, I am initiating a fresh discussion. Aside from utilizing axios, I also experimented with fetch but encountered similar outcomes. On the b ...

Utilizing .env Variables in NestJS Main App Module for Establishing Database Connection

I recently embarked on my first NestJS project, where I initially had a hardcoded database connection string in app.module.ts that worked perfectly fine. However, our project requirements called for fetching the database configuration values from environm ...

Achieving optimal performance with scoped CSS in Vue.js

As I work on creating a new app using VueJs, I have noticed the implementation of "css scoped" as shown below: <style scoped> .example { color: red; } </style> <template> <div class="example">hi</div> </template> ...

Transitioning the <mat-icon> from one <mat-form-field> to another may not always result in the icon being updated consistently

Being new to Angular, I have been working on creating a form for blog posting and I am trying to include selectable category icons next to the title. In my current setup, I have made a form with selectable font awesome icons. However, I am facing an issue ...

Encountering Issues with Accessing Property

Upon trying to run my code, the console is displaying an error that I am unable to resolve. The error specifically states: "TypeError: Cannot read property 'author' of undefined." View the StackBlitz project here The error seems to be coming fr ...

Bring in TypeScript property from an external scope into the current scope

I am encountering an issue with my TypeScript code. Inside the anonymous functions, I am unable to change the properties of the class because they are out of scope. Is there a way to pass them in so that they can be modified? class PositionCtrl { ...

Identifying browsers with Zend Framework versus JavaScript

Currently, I am working on developing an application that demands the capability to upload large files. After much consideration, I have opted to utilize the FormData object as it allows me to provide progress updates to the user. Sadly, Internet Explorer ...

Can someone break down the meaning of "returning $http.post" in Angular while developing a factory service?

I've been grappling with the concept of return $http.post('/some link') and can't seem to fully grasp it. Imagine I have a node/express backend and am utilizing Angular for my frontend. Within my api, there is a function like this: v ...

What is the best way to test the validity of a form while also verifying email availability?

I am currently working on implementing async validation in reactive forms. My goal is to disable the submit button whenever a new input is provided. However, I am facing an issue where if duplicate emails are entered, the form remains valid for a brief per ...

Mastering the art of utilizing CallBackScript effectively in Wicket 6.x

Back in the days of Wicket 1.x, I utilized an AjaxEventBehavior to implement a CallBackScript that provided me with the mouse coordinates. Here's a snippet of what I used: (getEventX() and getEventY() are JavaScript Functions) myObject.add(new Aj ...

Utilize Javascript to generate intricate table headers based on JSON data

I am currently facing a challenge in creating an HTML table header with colspan. I have a JSON object as follows: var metadata = [{ "colIndex": 0, "colType": "String", "colName": "PM" }, { "colIndex": 1, "colType": "String", "colName": "PR ...

Node.js may not always trigger the 'data' event when a successful POST request is made from a Java program

In my recent project, I encountered an issue with a NodeJs program that receives periodic data from a Java code. The problem is that sometimes only the header of the data makes it through, while other times both the header and the body successfully pass. H ...

Learning the ins and outs of Node.js: Creating a client to connect to a Node.js server and receive broadcast messages

In implementing my nodeJS Server, everything seems to be running smoothly. However, now I am looking to create a client that can receive messages from the server and trigger specific JavaScript functions based on those messages. The process involves: Us ...

Error: The lambda response was not defined. Please review your function code. The response returned with a status of 500 in 583 milliseconds

https://i.sstatic.net/GUHr9.png I'm experimenting with Netlify and its Lambda function feature to execute a Node.js function. Following the guide at https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget/. ...

How to efficiently update multiple documents in mongodb with mongoengine

I currently have a collection of dictionaries that look like this: {'data': '[{"url":"https://example1.com/photos-reviews/","score":0.671},{"url":"https://example1.com/concerts-events/concert-orga ...