Req.next is not a function

Asked by At

When I add the second post, I have this error:

Type error: req.next is not a function.

How can I fix it? I am using nodejs express with firebase realtime database

app.post("/addpost", urlencodedParser, function(req, res, next ) {
    console.log(typeof(req.next))
    if (req.session.user) {
        var db = firebase.database();
        var post = db.ref().child("/posts");
        var key = firebase
            .database()
            .ref()
            .child("/posts")
            .push()
            .getKey();
        console.log(key);

        post.push({
            name: req.body.name,
            title: req.body.title,
            content: req.body.content,
            subtitle: req.body.subtitle
        });

        //    var array = [value];
        //    console.log(array);
        //    post.set(array);
        res.redirect("/liststories");
      next();
      //  next();
    } else {
        res.redirect("/login");
    }
});

1 Answers

0
Vidya Sagar On

Next is used in middlewares where you have to pass control to the next middleware function or route.

Say you have a route,

app.get('/', function (req, res) {
  res.send('hello world')
})

Middleware for it be like,

app.use('/', function (req, res, next) {
  console.log('Request Type:', req.method)
  next()
})

So your code is supposed to be,

app.post("/addpost", urlencodedParser, function(req, res) {
    // console.log(typeof(req.next)) <--- Since there is no property next in Request Object
    if (req.session.user) {
        var db = firebase.database();
        var post = db.ref().child("/posts");
        var key = firebase
            .database()
            .ref()
            .child("/posts")
            .push()
            .getKey();
        console.log(key);

        post.push({
            name: req.body.name,
            title: req.body.title,
            content: req.body.content,
            subtitle: req.body.subtitle
        });

        //    var array = [value];
        //    console.log(array);
        //    post.set(array);
        res.redirect("/liststories");
      next();
      //  next();
    } else {
        res.redirect("/login");
    }
});

You can read more about middlewares here