Limpuls Limpuls - 3 years ago 193
JSON Question

Mongoose findOne function returns null/undefined

I always get

when fetching data from the database in my
route. Connection is successful and the user exists in the database (checked with Robo 3T). Here is an excerpt of the code:

var express = require("express");
var app = express();
var router = express.Router();
var mongoose = require("mongoose");
var user = require("../models/user");
var bodyParser = require("body-parser");
app.use(bodyParser.json());'/authenticate', function(req, res) {
user.findOne({ name: }).then(function(foundUser) {
if (!foundUser) { return res.status(404).send("No user matching name"); }
return res.status(200).json(foundUser);

Here is the request I'm executing from Postman:

enter image description here

Answer Source

Edited answer:

For express to parse form data in addition to json you need to add a second body parser to your application:

var app = require('express');
var bodyParser = require('body-parser');

// Enable express to parse body data from raw application/json data

// Enables express to parse body data from x-www-form-encoded data
app.use(bodyParser.urlencoded({ extended: false }));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download