0

while using an Ajax call I am trying to get data from the back-end

 componentDidMount: function () {
         $.ajax({
         url: 'http://127.0.0.1:3000/ap/test',
        dataType: 'json',
        success: function (data) {
            this.setState(data);
        }.bind(this),  error: function (xhr, status, err) {
            //   console.error(this.props.url, status, err.toString());
            console.error('failed to view user');
        }.bind(this)
    });

And here is my Get call in node.js

app.get('/test', function(req, res) {  DBs.find({},function(err,data){
if(err){ throw err;}
else {
  console.log('GET API CALLED, Data Sent!');
} sendingData = data;});   res.send(sendingData); });

1)API gets called but response isnt sent.('GET API CALLED, Data Sent!')

2)success function doesn't run in the Ajax call resulting in error: 'failed to view user'

1
  • Do you get a response from your node endpoint if you use curl? Commented Jul 29, 2017 at 21:16

2 Answers 2

1

Your DBs.find() call will be asynchronous and hence you need to send data from the callback function of DBs.find like

app.get('/test', function(req, res) {
    DBs.find({}, function(err, data) {
        if (err) {
            throw err;
        } else {
            console.log('GET API CALLED, Data Sent!');
            res.send(data);
        }

    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

Still not getting the response. Used res.send(data); like you said
can you console.log(data) in DB's callback, is it json
One more thing can you console.log(err) in ajax success call and share me the output
It runs but it's outputting nothing
Try logging xhr and see if you get something there
0

@ShubhamKhatri is right in saying that the call to res.send should be inside the callback function, so that it runs after the result of the database lookup is known.

But there's another bug: you forgot to convert the result to a JSON string before sending it. Then res.send expects a string (or maybe a buffer), so I suppose it just sends data.toString(), which would be [object Object] or something similar. Since you've used dataType: 'json', jQuery tries to read this as a JSON string. But it's not valid JSON, so the error function is called.

To fix this, just call JSON.stringify:

app.get('/test', function(req, res) {
    DBs.find({}, function(err, data) {
        if (err) {
            throw err;
            // BTW you might want less drastic error handling in this case. I'd say just send an HTTP 500 response and log something.
        } else {
            console.log('GET API CALLED, Data Sent!');
            res.send(JSON.stringify(data));
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.