本文介绍了无法在我的 Nodejs 中附加 res.send(result) 以将数据发送到 angular 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 Nodejs 和 angular2 应用程序,我需要将它们集成到 Neo4j 应用程序中.我可以从来自 Angular2 的 NodeJS 代码访问数据库,但随后我无法将该数据发送回 angular2 应用程序.如果我给

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 unable to send that data back to angular2 app. If i give

 res.send(result)

在我的函数 tmsServer 的任何地方,我都收到错误 -->

anywhere in my function tmsServer , i get error -->

 inside catch = Error: Can't set headers after they are sent.

请帮忙.我想将我的数据发送回 angular 应用.

Please help. I want to send my data back to angular app.

tmservercontroller.js

   // Require Neo4j
   var neo4j = require('neo4j-driver').v1;

  var path = require('path');
  var logger = require('morgan');
  var bodyParser = require('body-parser');
  var express = require('express');

  var router = express.Router();

   var app = express();


  // Create Driver
   const driver = new neo4j.driver("bolt://localhost:11001",
   neo4j.auth.basic("neo4j", "Virtuallib1"));



 // //View Engine

   app.set('views', path.join(__dirname, 'views'));

   app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(express.static(path.join(__dirname, 'public')));



  var session = driver.session();
  var request = require('request');

   var finalResult ;

   router.post('/', tmsServer);
   module.exports = router;


   function tmsServer(req, res) {

    session
       .run('MATCH (n) RETURN n LIMIT 5')

       .then(function (result){

           result.records.forEach(function(record){
               console.log("record = ", record);
               console.log("result = ", result)
               console.log("1]
   record._fields[0].properties=",record._fields[0].properties);


                    res.send(result);
           });


       })


       .catch(function(err){
        console.log("inside catch = "+err);
    })

    res.send('It Works');
    session.close();
   }

neo4j-primary.component.ts

   import { Component, OnInit } from '@angular/core';
   import { Injectable } from '@angular/core';

 import { ToasterService } from '../toaster.service';

  import { FormGroup, FormControl, FormBuilder, Validators } from
 '@angular/forms';
   import { Http, Response, Headers } from '@angular/http';
  import { config } from '../config';
  import { Subject } from 'rxjs';

  import 'rxjs/add/operator/map';
  import { map } from 'rxjs/operators';
  import 'rxjs/Rx';
  import { Observable } from 'rxjs';

  // Statics
  import 'rxjs/add/observable/throw';


 @Component({
  selector: 'app-neo4j-primary',
  templateUrl: './neo4j-primary.component.html',
  styleUrls: ['./neo4j-primary.component.css']
  })

 export class Neo4jPrimaryComponent implements OnInit {

 constructor(private http: Http,  private notify: ToasterService) { }

 ngOnInit() {
this.viewNodesStart();
    }


  emptyObj1;
   emptyObj;
   info;

   // -------------------------------  Nodes Entire Data -------------

  viewNodesStart() {

console.log("INSIDE viewNodesStart()")

// Nodes Value

   console.log("inside Nodes Value");
  var data = localStorage.getItem('token');

 console.log("data is=>",data+ "emptyobj1 = "+ this.emptyObj1);

   var url = config.url;
   var port = config.port;

   var object = {
    "emptyObj" : this.emptyObj
    }
    this.http.post("http://" + url+":" + port +
    "/viewNodesStart",this.emptyObj1)
  .map(Response => Response)
  .subscribe((res: Response) => {

    console.log("XXXXXXXXXXXX Response on /viewNodesStart", res);

    this.info = res;

    if (this.info.statusCode == 200) {
      console.log("Data added successfully");

    } else {
      console.log("Data is not inserted")

     }
     });

   }

   }

推荐答案

您只能将响应发送回一次,当您发送响应It works"并再次发送带有实际数据的响应时,您有一个异步操作正在运行.

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 将响应标头设置为对象,理想情况下应该在请求生命周期中发生一次.

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();
}

您的答案的解决方案是删除RES01.

The solution to your answer is to remove RES01.

这篇关于无法在我的 Nodejs 中附加 res.send(result) 以将数据发送到 angular 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:26