Continuously encountering an unhandled syntax error: identifier not as expected

This excerpt is from my app.js file for a mini express app I'm working on.

 var loginPost = function()

    user = {
        username: $('#username').val(),
        password: $('#password').val(),
    }; 

    $.ajax({
        url: 'http://localhost:3000/login',
        method: "POST",
        dataType: 'json',
        data: user
    }).done(function(data) {
        console.log(data.username+"login successful");

        user = Cookies.get("loggedinId");

        wishForm()

     });
};

I am encountering an uncaught syntax error on the line where it says user = {, but I can't seem to find anything wrong with it. Any clues or suggestions would be greatly appreciated. Thank you.

Answer №1

Don't forget the essential {

var loginPost = function(){   // This { is important

    user = {
        username: $('#username').val(),
        password: $('#password').val(),
    }; 

    $.ajax({
        url: 'http://localhost:3000/login',
        method: "POST",
        dataType: 'json',
        data: user
    }).done(function(data) {
        console.log(data.username+"login successful");

        user = Cookies.get("loggedinId");

        wishForm()

     });
};

Answer №2

Don't forget to include the braces and ensure you declare your variables to prevent contaminating the global scope (unless intentionally done).

Answer №3

You didn't catch

function(){ // These curly braces

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

The expected number of calls for the callback function was not met

Our employee webpage is designed to make Ajax calls to the node.js web server in a continuous loop. The problem arises when the callback UpdateTeamArr is expected to be triggered multiple times based on the loop max and the number of department options a ...

The map functionality is failing to render on the three.js object

function generate_geometry(scene) { var mesh; var material; var texture; var geometry; geometry = new THREE.BufferGeometry(); geometry.attributes = { position: { itemSize: 3, array: ...

Encountered an issue: Unable to access property 'findOne' on an undefined object within Node.js and Sequelize

I have completed the migrations and models in my admin_pages.js file, but I'm encountering the following error: TypeError: Cannot read property 'findOne' of undefined at C:\users\gaffer\desktop\gaffercart\ro ...

By harnessing a JSON response

After sending an ajax request, the server's response is as follows: {"error":false,"success":true} The ajax code used: $.ajax({ url: '/update', type: 'post', data: $(this).serialize(), success: function(response) ...

Delete the initial instance of a specific element with JavaScript or Lodash

I am working with an array that looks like this - ["a", "a", "b", "c", "d", "e"] My goal is to filter this array in a way that removes only the first occurrence of each element. Based on the exa ...

I am interested in incorporating various forms within this code

I followed an online tutorial to create this code, and I've made some edits myself, mainly related to the buttons that display information. However, I'm still learning and struggling with adding different forms to each section. I would really app ...

What is the best way to choose all checkboxes identified by a two-dimensional array?

I need help with a question div setup that looks like this: <div class="Q"> <div id="Q1"><span>1. </span>Which of the following have the same meaning?</div> <div class="A"><input type="checkbox" id="Q1A1Correct" /& ...

The status of the AWS ElasticBeanStalk environment is showing as SEVERE even though the health check is returning a

I have a specific code written in ExpressJS deployed on AWS Elastic Beanstalk with an Application Load Balancer. The access.log file located at /var/log/nginx/ is showing the following information: {IP ADDRESS} - - [10/Jan/2022:00:52:06 +0000] "GET / ...

Utilize Next.js and GSAP to dynamically showcase images upon hovering over the title

I have a dynamic list of titles that I want to enhance by displaying images when hovering over each title. The issue I'm facing is that when I hover over one title, all the images display at once. As a React beginner, I believe the solution should be ...

Manage image placement using CSS object-position

I have the following code snippet: img{ width: 100%; height: 1000px; object-fit: cover; object-position: left; } <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

The use of res.sendFile() in node.js is not recognized

While utilizing node.js along with mySQL, I encountered a problem. Upon starting the server, everything seems to be fine. However, upon accessing 127.0.0.1:3000, an error message stating that res.sendFile() is not defined appears. My intention is to send ...

Creating a standalone module using webpack that can be accessed through a script element

When setting the library target to 'umd' in the package.json file, the expectation was that the outputs would be usable via a <script> tag and if no module system was available it would be attached to the window. However, this does not seem ...

An error occurred when attempting to search for 'length' using the 'in' operator in the context of the Datatables plugin and jQuery 1.11.3

I implemented the jQuery Datatables plugin in my tables for pagination, sorting, and searching functionalities. However, I am facing issues where the elements are not working properly, and the pagination occasionally fails to display. The Chrome console is ...

Ensuring that only one field is selected with mandatory values using Joi validation

Is there a way to create a validation rule utilizing Joi that ensures if valueA is empty, then valueB must have a value, and vice versa? My current approach involves using Joi for validating an array of objects with properties valueA and valueB. Below is ...

Leveraging async/await in express

I am encountering an issue with my app.post method while trying to deploy on Firebase. The error message reads: Parsing error: Unexpected token =>. I am fairly new to node.js and Javascript as I primarily work with Swift. However, I require this code fo ...

Accessing a value within a JSON body response can be done by utilizing the Request module to send GET requests to an external API

Recently, I started working on a project using Express and Node.js to build a simple API that fetches data from an external source. When I run the code provided below and input my /api/posts/tech endpoint into Insomnia, I get back a response containing a J ...

Troubleshooting JSONP Implementation with JQuery: Scope versus Async Problems

I've been trying to call a JSONP service using the $.ajax function: $.ajax({ url: jsonpURI, dataType: "jsonp", jsonpCallback: callback, success: function () { console.log("Success"); }, error: function (err) { ...

Utilizing the HasMany - BelongsTo relationship in Ember.js with the RESTAdapter

Can anyone provide guidance on creating a has many belongs to relationship using RESTAdapter in EmberCLI? I am working on a project where a card (representing a Twitter user) can have multiple hashtags associated with it. Here are my model definitions: / ...

Designing an opening interface for an HTML5 Canvas gaming experience?

It's common knowledge that the HTML5 Canvas element interfaces seamlessly with the incredibly efficient Javascript Engines found in Firefox, Safari and Chrome. Lately, I've been delving into game development using Javascript and the Canvas, and ...

Is it possible to use one HTML button to submit a token and create a charge in Stripe?

<!-- Payment Form --> <form action="/payment" method="post" id="payment-form"> <div class="form-row"> <label for="card-element"> Ent ...