Node.js FIREBASE警告:用户回调引发异常。错误:Can';t在发送邮件后设置邮件头

Node.js FIREBASE警告:用户回调引发异常。错误:Can';t在发送邮件后设置邮件头,node.js,rest,express,firebase,Node.js,Rest,Express,Firebase,我们正在开发一个应用程序,其中我们使用Firebase作为数据库,express作为路由RESTful API的中间件/后端,RESTful API由我们在Reactjs中开发的前端使用 下面是我们的server.js文件的外观: var express = require('express'); var app = express(); //Used for getting POST variables from forms as well as query parameters var b

我们正在开发一个应用程序,其中我们使用Firebase作为数据库,express作为路由RESTful API的中间件/后端,RESTful API由我们在Reactjs中开发的前端使用

下面是我们的server.js文件的外观:

var express = require('express');
var app = express();

//Used for getting POST variables from forms as well as query parameters
var bodyParser = require('body-parser');
var validator = require('express-validator');

//Contains all the routes
var routes = require('./routes/routes');
var path = require('path');

//Used for serving jade files
app.set('view engine', 'jade');
//For serving static resources like scripts, styleSheets, html
app.use(express.static(__dirname + '/views'));

app.all('/*', function(req, res, next) {
 res.header("Access-Control-Allow-Origin", "*");
 res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept");
 res.header("Access-Control-Allow-Methods", "POST, GET");
 next();
});

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(validator({
    customValidators: {
        onlyAlphabets: function(value) {
            if (value.match('^[a-zA-Z ]*$')) {
                return true;
            } else {
                return false;
            }
        },
        password: function(value) {
            if (value.length >= 6 && value.length <=25 && value.match('^[\x20-\x7F]*$')) {
                return true;
            } else {
                return false;
            }
        }
    }
}));
app.use(routes);

var port = process.env.PORT || 8080; //Set our port

app.listen(port);
console.log('Magic happens on port ' + port);
下面是我们在前端添加的用于发布数据的代码。我们还使用whatwg fetch包:

httpservice.js:

var Fetch = require('whatwg-fetch');
var baseUrl = 'http://192.168.1.134:8080';

var Service = {
  get: function(url) {
    console.log('MAKING A GET REQUEST');
    return fetch(baseUrl + url)
    .then(function(response) {
      return response.json();
    });
  },

  post: function(url, postData) {
    console.log('MAKING A POST REQUEST');
    return fetch(baseUrl + url, {
      headers: {

        'Content-Type': 'application/json'
      },
      method: 'POST',
      body: JSON.stringify(postData)
    }).then(function(response) {
      return response;
    });
  }
}

module.exports = Service;
VendorForm.js(React组件文件)

我们通过nodemon启动服务器,该服务器提供FIREBASE+EXPRESS中开发的RESTful API。这是我们在发布以下内容时收到的错误:

FIREBASE WARNING: Exception was thrown by user callback. Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at ServerResponse.header (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:163:12)
    at ServerResponse.json (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:249:15)
    at ServerResponse.send (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:151:21)
    at /var/www/tutorials/express_firebase/routes/routes.js:30:12
    at /var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:200:710
    at ec (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:52:165)
    at ac (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:31:216)
    at bc (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:30:1259)
    at Ji.h.Mb (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:220:440)
    at X.set (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:256:335)
    at /var/www/tutorials/express_firebase/routes/routes.js:96:24
    at Layer.handle [as handle_request] (/var/www/tutorials/express_firebase/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/tutorials/express_firebase/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/var/www/tutorials/express_firebase/node_modules/express/lib/router/route.js:112:3) 
/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:52
(d="0"+d),c+=d;return c.toLowerCase()}var zd=/^-?\d{1,10}$/;function td(a){return zd.test(a)&&(a=Number(a),-2147483648<=a&&2147483647>=a)?a:null}function ec(a){try{a()}catch(b){setTimeout(function(){R("Exception was thrown by user callback.",b.stack||"");throw b;},Math.floor(0))}}function S(a,b){if(t(a)){var c=Array.prototype.slice.call(arguments,1).slice();ec(function(){a.apply(null,c)})}};function Ad(a){var b={},c={},d={},e="";try{var f=a.split("."),b=Pb(id(f[0])||""),c=Pb(id(f[1])||""),e=f[2],d=c.d||{};delete c.d}catch(g){}return{oh:b,Dc:c,data:d,ah:e}}function Bd(a){a=Ad(a).Dc;return"object"===typeof a&&a.hasOwnProperty("iat")?z(a,"iat"):null}function Cd(a){a=Ad(a);var b=a.Dc;return!!a.ah&&!!b&&"object"===typeof b&&b.hasOwnProperty("iat")};function Dd(a){this.Y=a;this.g=a.n.g}function Ed(a,b,c,d){var e=[],f=[];Na(b,function(b){"child_changed"===b.type&&a.g.Ad(b.Le,b.Ma)&&f.push(new H("child_moved",b.Ma,b.Ya))});Fd(a,e,"chi

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at ServerResponse.header (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:163:12)
    at ServerResponse.json (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:249:15)
    at ServerResponse.send (/var/www/tutorials/express_firebase/node_modules/express/lib/response.js:151:21)
    at /var/www/tutorials/express_firebase/routes/routes.js:30:12
    at /var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:200:710
    at ec (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:52:165)
    at ac (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:31:216)
    at bc (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:30:1259)
    at Ji.h.Mb (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:220:440)
    at X.set (/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase-node.js:256:335)
    at /var/www/tutorials/express_firebase/routes/routes.js:96:24
    at Layer.handle [as handle_request] (/var/www/tutorials/express_firebase/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/tutorials/express_firebase/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/var/www/tutorials/express_firebase/node_modules/express/lib/router/route.js:112:3)
[nodemon] app crashed - waiting for file changes before starting...
FIREBASE警告:用户回调引发异常。错误:发送邮件后无法设置邮件头。
在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:344:11)
位于ServerResponse.header(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:718:10)
在ServerResponse.send(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:163:12)
在ServerResponse.json(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:249:15)
在ServerResponse.send(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:151:21)
在/var/www/tutorials/express_firebase/routes/routes.js:30:12
位于/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:200:710
在ec(/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:52:165)
在ac(/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:31:216)
在bc(/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:30:1259)
在Ji.h.Mb(/var/www/tutorials/express\u firebase/node\u modules/firebase/lib/firebase node.js:220:440)
在X.set(/var/www/tutorials/express\u firebase/node\u modules/firebase/lib/firebase node.js:256:335)
在/var/www/tutorials/express_firebase/routes/routes.js:96:24
在Layer.handle[作为handle\u请求](/var/www/tutorials/express\u firebase/node\u modules/express/lib/router/Layer.js:95:5)
接下来(/var/www/tutorials/express\u firebase/node\u modules/express/lib/router/route.js:131:13)
在Route.dispatch(/var/www/tutorials/express\u firebase/node\u modules/express/lib/router/Route.js:112:3)
/var/www/tutorials/express\u firebase/node\u modules/firebase/lib/firebase node.js:52
(d=“0”+d),c+=d;返回c.toLowerCase()}var zd=/^-?\d{1,10}$/;函数td(a){return zd.test(a)&&(a=Number(a),-2147483648=a)};(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)a(a)b(a)a(c)b(a)b(a)a(d(d(f(f[1)a)c(a)a)c(c)a,c={{},c,c,c,c,c(c)a,c(c)a,c,c)a,c(a,c)a,c(a,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,Cd(a){a=Ad(a);var b=a.Dc;return!!a.ah&&!!b&&“object”==b的类型和b.hasOwnProperty(“iat”)};函数Dd(a){this.Y=a;this.g=a.n.g}函数Ed(a,b,c,d){var e=[],f=[];Na(b,函数(b){“child_changed”==b.type&&a.g.Ad(b.Le,b.Ma)&&f.push(new H(“child_moved,b.Ma,b.Ya));Fd(a,e,“chi
错误:发送邮件后无法设置邮件头。
在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:344:11)
位于ServerResponse.header(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:718:10)
在ServerResponse.send(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:163:12)
在ServerResponse.json(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:249:15)
在ServerResponse.send(/var/www/tutorials/express\u firebase/node\u modules/express/lib/response.js:151:21)
在/var/www/tutorials/express_firebase/routes/routes.js:30:12
位于/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:200:710
在ec(/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:52:165)
在ac(/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:31:216)
在bc(/var/www/tutorials/express_firebase/node_modules/firebase/lib/firebase node.js:30:1259)
在Ji.h.Mb(/var/www/tutorials/express\u firebase/node\u modules/firebase/lib/firebase node.js:220:440)
在X.set(/var/www/tutorials/express\u firebase/node\u modules/firebase/lib/firebase node.js:256:335)
在/var/www/tutorials/express_firebase/routes/routes.js:96:24
在Layer.handle[作为handle\u请求](/var/www/tutorials/express\u firebase/node\u modules/express/lib/router/Layer.js:95:5)
接下来(/var/www/tutorials/express\u firebase/node\u modules/express/lib/router/route.js:131:13)
在Route.dispatch(/var/www/tutorials/express\u firebase/node\u modules/express/lib/router/Route.js:112:3)
[nodemon]应用程序崩溃-正在等待文件更改,然后再启动。。。

根据错误,我们知道一些回调函数设置了两次标头,但不确定它是如何发生的。我们遇到了一些堆栈溢出问题,但仍然没有找到解决方案。如果您看到堆栈跟踪,我们将不胜感激。提前感谢。

at /var/www/tutorials/express_firebase/routes/routes.js:30:12
这是文件route.js中的第30行

response.send(Object.keys(store).map(function(k) { return store[k]; }));
您需要在上为更改一次方法,否则每次数据更新时都会触发回调函数

router.route('/get_vendors').get(function(request, response) {
  myFirebaseRef.on("value", function(snapshot) {
在调用post方法时中断响应,因为头已经在第30行中发送

参见参考文献


基本上,您使用
once
方法所做的是在读取值后立即删除回调。

myFirebaseRef.on(“value”,fn)是否多次激发?@BenFortune根据您的建议,我在myFirebaseRef.on(“value”,fn)中做了一个控制台。它
response.send(Object.keys(store).map(function(k) { return store[k]; }));
router.route('/get_vendors').get(function(request, response) {
  myFirebaseRef.on("value", function(snapshot) {