Unable to send data using GET method after implementing passportjs integration

In the route.js file, I have implemented the following REST method:

app.get('/api/todos', isAuthenticated, function(req, res) {
    DB.TodoTable.find()
    .exec(function(err, todos) {
        res.json(todos, function(err){
            if (err) console.log(err);
        });     // return all todos in JSON format

        console.dir(res);
    });
});

Upon debugging, it was discovered that the 'res' object's 'statusCode' field contains unexpected data. This could be the reason why the data is not being received on the angular side.

{
    ...
    statusCode:
     [ { index: Mon Jan 19 2015 09:33:54 GMT+0530 (India Standard Time),
         text: 'hello',
         _id: 23ffs2sdfsdd64c0834534,
         __v: 0 } ] 
}

In controller.js, there is another method:

Todos.get()
    .success(function(data) {
        console.info("Data received from server:");
        console.dir(data);
    });

However, in this case, 'data' is returned as an empty string. This issue seemed to arise after integrating passport.js.

Is passport.js causing this problem? If not, what steps can be taken to resolve this issue?

Answer №1

By making a small adjustment and removing the callback from the 'res.json' method, I was able to get this functionality up and running smoothly.

In this particular scenario, it appears that the callback was causing some unexpected behavior. The exact reason for this is still unknown to me.

However, by modifying the code as follows:

app.get('/api/todos', isAuthenticated, function(req, res) {
    DB.TodoTable.find()
    .exec(function(err, todos) {
        res.json(todos);     // return all todos in JSON format

        console.dir(res);
    });
});

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

Invoke a function in a different component following the completion of an asynchronous task in a React.js application

I am working with 2 components in my project. The first component contains a function that needs to be called after an async function in the second component completes. I am looking for a way to achieve something similar to Vue's this.$emit() function ...

What exactly happened to my buttons when I transition to the mobile display?

Due to time constraints, I decided to save some time by utilizing a CSS template called Horizons created by Templated. This particular CSS template uses Javascript to adjust the layout for mobile devices, causing buttons to switch from inline to block for ...

Fixing Timezone Issue in VueJs 3 with Axios POST Request

Having an issue with axios. In my application, I have an input field for datetime-local. When I select a date and time, the displayed time is correct (e.g., selecting 10:00 shows as 10:00), but when calling a function using axios.post, the request sends th ...

Steps for creating a sleek vertical slider with transitional effects using JavaScript and HTML

Take a look at my code snippet: <table> <tr> <td onclick="shownav()">Equations of Circle <div id="myDownnav" class="sidenav"> <a>General Info</a> <a>In Pola ...

Discovering the highest value within an array of objects

I have a collection of peaks in the following format: peaks = 0: {intervalId: 7, time: 1520290800000, value: 54.95125000000001} 1: {intervalId: 7, time: 1520377200000, value: 49.01083333333333} and so on. I am looking to determine the peak with the hig ...

The set command is failing to save data in the JSON file

One of my new commands, known as "muterole", includes various subcommands. Here's how the muterole command is structured: @commands.group(aliases=['mr']) @commands.has_permissions(manage_roles=True) async def muterole(self, ctx): if ctx. ...

gRaphael - the struggle of animating a line chart

Encountering an issue with the gRaphael javascript line chart library. Creating a line chart from a CSV file with five columns (# of minutes, time, waiting time, in treatment, closed, in location). Previously drew full chart without animation successfull ...

Enumerate the variable values that are validated using jQuery

I've made good progress with this in jsFiddle, but I can't seem to figure out why the first value isn't displaying a bullet point.. Check out my code on jsFiddle! <div id="checkboxes"> <input id="chkbx_0" type="checkbox" name= ...

Escape sequences do not seem to be functioning properly when using innerHTML

I am facing an issue where a string containing HTML escape characters (such as < and >) needs to be rendered inside a div using innerHTML. The intention is for the escaped characters to appear as text rather than as actual HTML, but they still render ...

Using Rails: How to invoke a function in the asset pipeline from a JS response?

In one of the JavaScript files I am working with, I have defined an object and a function: chosen.coffee Foo = do_this: -> $('.slider').slider() $ -> Foo.do_this() This code initializes JQueryUI to turn a specific div into a ...

Looking to display or conceal several divs according to the option selected from a dropdown menu?

I've been searching for a solution to my simple dropdown issue, but haven't had any luck in the forums. This is the code for the dropdown: <select id ="category_faq"> <option value="1">item1</option> <option value= ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

Is there a way to redirect using Express JS when the destination is

Hello there, I'm encountering an issue with redirecting in express js. I have a function that should trigger when a submit button is pressed and then redirect to a different page. However, when using res.redirect('/results.ejs');, I receive ...

Guidelines for setting up Kendo notifications using an angularjs service

I have successfully defined the Kendo notification in my Angular service. However, when I try to use it with the line uiService.notify.error("You missed some required fields.");, I am getting an error that says "Cannot read property 'show' of nul ...

Formatting code within an HTML document

I am attempting to customize a stock widget that I obtained from financialcontent.com. My current approach involves using Bootstrap for styling purposes. To incorporate the widget into a div, I found it necessary to embed the script directly within the HT ...

Is there a way to display my array within my React component using JavaScript?

I am working with an element that looks like this: const DropdownElements = [ { key: 1, title: "City", placeholder: "Select City", apiUrl: "https://api.npoint.io/995de746afde6410e3bd", type: "city&qu ...

Tips for testing "defineAsyncComponent" in Vue 3:

Suppose I need to unit test this utility function. I am utilizing Vue 3, however, this code resides in a "regular" JavaScript file rather than an SFC. How can I go about doing that? function getDynamicComponent() { if (...) { return defineAsyncComp ...

Include a personalized header when making an HTTP GET request with AngularJS

This is my angularjs request. var req = { method: 'GET', url: 'http://localhost:8080/test', headers: { "x-auth-token" : user.token } } ...

What could be causing the issue preventing me from updating my SQL database through AJAX?

$(document).ready(function(){ $('.button').click(function(){ var clickBtnValue = $(this).val(); var ajaxurl = 'functions/delivered.php', data = {'action': clickBtnValue}; $.post(ajaxurl, da ...

Using jQuery to Post JSON

I need help with implementing an ajax call that sends data in JSON format. There seems to be a syntax error in my code. For example: var string_json='{"usr":{"name":"john","surname":"do"}}'; $.ajax({ url:"myurl",type:"post",data:{user:JSON.strin ...