Waiting for the API endpoint to be posted on the development tool

After creating login credentials in the database, I am facing an issue when trying to use them to log in. The network response for the /api/login endpoint remains stuck on a pending request. Upon checking the response, it seems that the payload data intended to be sent back to MongoDB is not going through.

I attempted to use $q defer promise in the vm.loginuser controller where the call is made, but to no avail. Even using Postman for the login process results in a pending request.

Here is the Angular Controller code:

vm.loginUser = function () {
        $http.post('/api/login', vm.userlogin).success(function(response){
            console.log('Redirecting to profile');
        }).error(function(error){
            console.log('Error');
        });
    };

When I try using .then instead of .success, I encounter an error stating "then" is undefined and

localhost:3000/[object%20Object] 404 (Not Found)

The server.js code for calling the login endpoint:

app.post('/api/login', authController.login);

Module: This console.log message appears in the command prompt, and when I include the entire code, the API request gets stuck on pending. Not sure if there's an issue with the code or if MongoDB is taking a long time to return the username and password.

module.exports.login = function (req, res){
   res.send('test'); // test successful 
   User.find(req.body, function(err, results){
    if(err){
        console.log('Failed to login')
    }

    if(results && results.lenght ===1){
        res.json(req.body.username);
    }
 })
}

The HTML form for login:

<input type="text" class="form-control" id="username" 
    placeholder="Username" ng-model="vm.userlogin.username">

<input type="password" class="form-control" id="exampleInputPassword1" 
    placeholder="Password" ng-model="vm.userlogin.password">

<button type="submit" class="btn btn-default" 
    ng-click="vm.loginUser()">Submit</button>

Answer №1

Would you be able to try running this code snippet for your Angular login request?

$http.post('/api/login', vm.userlogin)
.then(function(success) {

    console.log("SUCCESS");
    console.log(success);

}, function(err) {

    console.log("ERROR");
    console.log(err);

})
.finally(function() {

    console.log("FINALLY");

});

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 children of the KendoUI treeview are showing up as null

Within my KendoUI treeview setup, the main nodes trigger a call to an MVC controller that checks for a nullable ID parameter before utilizing a different model. When accessing the URL: http://localhost:2949/Report/GetReportGroupAssignments The resulting ...

Error: The client must be connected before any operations can be performed in a production environment

import mongoose from "mongoose"; let connection = {}; async function establishConnection() { try { if (connection.isConnected) { console.log('Connected to database'); return; } if (mongoose.connections.length ...

The issue of the "port" attribute not working for remotePatterns in the Image component has been identified in Next.js 13's next.config.js

I've encountered an issue with the code snippet below. I'm attempting to utilize remotePatterns in my next.config.js file to enable external images. Strangely, when I set the port to an empty string "", it functions correctly. However, specifying ...

Is there a way to prevent the onClick event from executing for a particular element in React?

Currently working with Material UI, I have a TableRow element with an onClick event. However, I now need to incorporate a checkbox within the table. The checkbox is enclosed in a TableCell element, which is nested within the TableRow. The issue arises wh ...

Build a custom Angular2 pipe to convert JSON data into an array through iteration

I am attempting to take the JSON data provided below and convert it into an array for use with *ngFor='let item of items', which will allow me to display data as item.name, etc... This is what I have tried: var out = []; for(var key1 in object) ...

Enable the parsing of special characters in Angular from a URL

Here is a URL with special characters: http://localhost:4200/auth/verify-checking/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="59663c34383035643230383d2b606a6e6b686d6e6e193e34383035773a3634">[email protected]</a> ...

What is the best way to retrieve an AJAX response in advance of sending it to a template when utilizing DATAT

Recently, I've been working on integrating log tables into my admin panel using the datatable plugin. Despite setting up an ajax call in my datatable, I'm facing issues with retrieving the response before sending it to the table. Here's a s ...

Ensuring data accuracy with form validation and interactive features with Ajax using Vanilla

I am working on a form that requires validation before submission, and I need to display error notifications without refreshing the page. Below is the code for the form: <button name="save" type="submit" form="product_form" ...

Enhancing class names in production mode with Material UI, Webpack, and React to optimize and minimize code size

webpack - v4.5+ material ui - v4.9.7 react - v16.12.1 Ordinarily, all classes should follow the pattern of the last one in the first example. However, for some unknown reason, many classes remain unchanged in production mode. Any thoughts on this issue? ...

Node.js backend includes cookies in network response header that are not visible in the application's storage cookies tab

I am currently working on creating a simple cookie using express's res.cookie() function. However, I am facing an issue where the cookies are not being set correctly in the application tab. My project setup includes a React frontend and a Node backend ...

What is the best way to save the current state data in AngularJS?

Encountering an issue here. To view the problem, check out this Plunker link -> In my Angular JS project, I need to save the current state data somewhere other than local storage. On one page, I have a list of models (cards), each containing two tabs. ...

discovering the nearest preceding sibling that includes the class .myClass

I have multiple <tr> elements, some of which contain a <td> with the class class="myClass" and some do not. Here is an example of how it may look: <tr> <td class="myClass"></td> <td></td> </tr> <tr> & ...

What is the best practice for storing a SQLITE database file in electron when in production mode?

Currently, I am developing a portable Node.js server using Electron and utilizing an SQLITE database for data storage. During development, the database file test.db is placed in the same directory as my Main.js file, which works perfectly. However, when I ...

What is the best way to retrieve data based on conditions in React?

I'm currently learning React and trying to pass props from a parent component (table row) to a child Modal component. Inside the child component, I want to fetch data based on the props provided. I have created a custom hook called useFetch that store ...

Passing arguments to the callback function in React: a comprehensive guide

Within my react component, I have a collection of elements that I want to make clickable. When clicked, I trigger an external function and pass the item ID as an argument: render () { return ( <ul> {this.props.items.map(item => ( ...

How can you utilize Javascript to retrieve an element by its inner text within the HTML tags?

As a novice, I have been struggling to find the solution to my question even after extensive searching. In the scenario at hand, I am specifically interested in locating the following element: <a href="bla" onclick="dah">some text here</a> I ...

Utilize AngularJS to sort data based on specific columns

I have a table with two columns: "Name" and "Description". In addition, I have a dropdown list containing the names of these two columns. The user needs to select one item from the dropdown list to indicate which column should be filtered, and then enter ...

I am curious to know how I can utilize Node.js to sum up values from a specific column within a CSV file

I recently started working with node.js and I'm currently working on a side project that I'm having some trouble with. My goal is to extract values from a specific column in an unzipped csv file and then add them up using node.js. Below is my co ...

Having trouble transmitting information to a php script using $.post()?

I have set up a shopping cart functionality on my website. Here's how it works: On the catalog page, there are four products, each with its own "Add to Cart" button. When a user clicks on one of these buttons, an onClick attribute calls the addToCart( ...

Socketio: Issue: Isolated surrogate U+D83D is not a valid scalar value

I've been experiencing frequent crashes with my node.js server recently, all due to a recurring socket.io error. It seems that the client may be sending invalid UTF strings, causing an error in the utf8.js file. I'm getting frustrated with the co ...