wsfuller
wsfuller

Reputation: 1840

Node server response error: process.nextTick(function(){throw err;});

So I have a very simple application that I'm building to practice with Mongo, Express, and Node.

I'm getting an error process.nextTick(function(){throw err;});

The error seems to occur when I try to use res.json(docs) in the success conditional during the GET request. However, I can console.log(docs) and see the JSON.

Development Environment

package.json:

...
  "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4",
    "mongojs": "^2.3.0"
  }
...

app.js (api):

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('catalog', ['products']);

app.get('/', function(req,res){
  res.send('It works, indeed'); // this is working
});

app.get('/products', function(req,res){
  res.send('Products Works'); // this is displaying correctly on the page
  console.log('Fetching products');
  db.products.find(function(err,docs){
    if(err){
      res.send(err);
    }
    else{
      console.log('Sending Products');
      console.log('response docs', docs); // returns the correct JSON
      res.json('stuff sent'); // Throws error
    }
  });
});

app.listen(3000);
console.log('Server is running on port 3000');

I'm not sure why this is failing. I'm able to console log the JSON so know that the data is there and available in the response. Just not sure why the res.json(docs) doesn't work.

Upvotes: 1

Views: 1129

Answers (1)

eljefedelrodeodeljefe
eljefedelrodeodeljefe

Reputation: 6791

You are calling the implicit callback twice

res.send('Products Works'); // this is displaying correctly on the page

and

res.json(docs);

remove either (probably the first one)

Upvotes: 1

Related Questions