Unable to attach an res.send(result) in my Nodejs to send data to angular app

前端 未结 1 539
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 20:11

I have made a Nodejs and angular2 app where i need to integrate them to Neo4j app. I am able to hit the Database from my NodeJS code coming from Angular2 but then i am unabl

相关标签:
1条回答
  • 2021-01-16 20:37

    You can send the response back ONLY once, you have an asynchronous operation running while you send the response "It works" and again sending the response with actual data.

    res.send sets response headers to object which should ideally happen once during the request lifecycle.

    function tmsServer(req, res) { 
        session
         .run('MATCH (n) RETURN n LIMIT 5')
         .then(function (result){
           result.records.forEach(function(record){
             // This will execute only when the promise is resolved and data is returned from database.
             res.send(result); // (RES02)
           });
         })
         .catch(function(err){
          console.log("inside catch = "+err);
         })
    
         res.send('It Works'); // (RES01) <--- This runs before RES02
        session.close();
    }
    

    The solution to your answer is to remove RES01.

    0 讨论(0)
提交回复
热议问题