简体   繁体   中英

How to use res.send() in Node.js

I am using mongoose api with a Node.js/Express application. I have a get request on the client side that asks for some data, but for some reason that data never makes it to the client side. What is wrong?

Snippet of backend

app.get('/up', isLoggedIn, function(req,res){
    req.user.local.level = 2;
    req.user.save(function(err){
        res.send(req.user.local.level);
    });

});

Snippet of Frontend

function try(){
    var ks=1;
    if(true){
            $.get('/up', function(data){
                ks=data;    
            });
        }
        console.log(ks);
    }

The server console displays 2, yet the browser console still displays 1, What is wrong?

Edit
I took your advice but it still wont work. What am i doing wrong?

var ks=1;
if(true){
    jQuery.ajax({
         url:    '/up' ,
         async:   false,
         success: function(data) {
                    ks = data;
                    console.log(ks);
                  }

    });
    }
    console.log(ks);

发送响应后,该代码将在您发送请求后立即运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM