错误:当我第二次使用标头后,无法设置标头

时间:2018-07-06 07:43:36

标签: node.js express

添加类别时,第二次出现此错误。第一次按预期运行。

代码:

app.post("/addcategory", function(req, res) {
    var firebaseUser = firebase.auth().currentUser;
    if (firebaseUser) {
        var catname = req.body.categoryName;
        console.log("catname", catname);
        var categoryName = catname.replace(/\s/g, '');
        console.log("categoryName", categoryName);
        var fbuser = firebaseUser.uid;
        var categories = new Array();
        docref = db.collection(fbuser).doc("menu");

        // creating a transaction
        var trans = db.runTransaction(t => {
            return t.get(docref).then(doc => {
                    var newCategories = new Array();
                    console.log("db" + doc.data().categories);
                    newCategories = doc.data().categories;
                    console.log("newCategories " + newCategories);
                    newCategories.push(categoryName);
                    console.log("newCategories after push " + newCategories);
                    t.update(docref, { categories: newCategories });
                });
        }).then(result => {
            console.log("transaction success");
            res.redirect("/categories");
        }).catch(error => {
            console.log("transaction failed", error);
        });

    } else {
        console.log("no login");
    }
});

我在控制台上遇到的错误是:

Error: Can't set headers after they are sent.
  at validateHeader (_http_outgoing.js:489:11)
  at ServerResponse.setHeader (_http_outgoing.js:496:3)
  at ServerResponse.header (C:\Users\vishesh\node\Success\node_modules\express\lib\response.js:767:10)
  at ServerResponse.send (C:\Users\vishesh\node\Success\node_modules\express\lib\response.js:170:12)
  at done (C:\Users\vishesh\node\Success\node_modules\express\lib\response.js:1004:10)
  at tryHandleCache (C:\Users\vishesh\node\Success\node_modules\ejs\lib\ejs.js:257:5)
  at View.exports.renderFile [as engine] (C:\Users\vishesh\node\Success\node_modules\ejs\lib\ejs.js:472:10)
  at View.render (C:\Users\vishesh\node\Success\node_modules\express\lib\view.js:135:8)
  at tryRender (C:\Users\vishesh\node\Success\node_modules\express\lib\application.js:640:10)
  at Function.render (C:\Users\vishesh\node\Success\node_modules\express\lib\application.js:592:3)
  C:\Users\vishesh\node\Success\node_modules\express\lib\response.js:1003

  if (err) return req.next(err);
                    ^

TypeError: req.next is not a function
  at done (C:\Users\vishesh\node\Success\node_modules\express\lib\response.js:1003:25)
  at tryRender (C:\Users\vishesh\node\Success\node_modules\express\lib\application.js:642:5)
  at Function.render (C:\Users\vishesh\node\Success\node_modules\express\lib\application.js:592:3)
  at ServerResponse.render (C:\Users\vishesh\node\Success\node_modules\express\lib\response.js:1008:7)
  at Timeout.setTimeout [as _onTimeout] (C:\Users\vishesh\node\Success\app.js:116:29)
  at ontimeout (timers.js:469:11)
  at tryOnTimeout (timers.js:304:5)
  at Timer.listOnTimeout (timers.js:264:5)

0 个答案:

没有答案