1

I have the following code

app.get('/posts',function(req,res){

console.log(posts);
res.send(posts);
res.send(200);
});

And i am using the following to get and return the array of js objects (posts is the array)

App.PostsRoute = Ember.Route.extend({
model: function(){
    return $.ajax({
        url : '/posts',
        type : 'GET',
        success : function(data){
            return data;
        }
    });
}
});

So when i populate the posts array with [ { body: "Hello" }, {body : "world" } ] , i get the following output in console :

Output of node console

Whereas in my app, the model is not rendered, moreover i can't see any response in chrome dev tools, these are some ss

Chrome SS

Response is Empty, not even {} . What is going wrong? I dont think there is anything with Ember , after all i can't see the response!

2 Answers 2

1

I think the problem occurs because you call res.send twice. Just comment this line:

res.send(200);

This line is not needed because Express automatically sets status code as 200 (or in some occasions - 304).

If you want to set your own status code, just write:

res.status(someCode);

as you can read here.

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

2 Comments

but will it work if use res.send(200) and res.json(obj) one after another?
@user206032 i believe not will not. but you can also write either res.json(200, obj);, or res.send(200, obj);
1

A few problems:

  1. You need to JSON.stringify your object before you send it.

  2. Try to use this instead of res.send:

    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(posts));
    
  3. Where are you trying to return 'data'? You should have some callback function there, like this:

    success:function(data) {
        handleData(data); 
    }
    

1 Comment

the data is returned as model for postsRoute, it is Ember

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.