mongoose.findById does not return anything and request keep hanging
See original GitHub issuesystem specifications
- node v 6.1.0
- npm v 3.8.6
- “mongodb”: “^2.1.18”
- “mongoose”: “^4.4.16”
- ubuntu 16.04
created schema and added one entry by terminal using db.locations.save({...});
get the id of that entry and tried to use in my api but does not return anything when hitting api/locations/:locationid in browser
app_api//models/db.js
var mongoose = require( 'mongoose' );
var mongoURI = 'mongodb://localhost/loc8r';
var mongoDB = mongoose.createConnection(mongoURI);
mongoDB.on('connected', function (){
console.log('mongoose connected to ' + mongoURI);
});
require('./locations');
app_api/models/locations.js
var mongoose = require( 'mongoose' );
var Schema = mongoose.Schema;
var locationSchema = new Schema({
name: {type: String, required: true},
address: String,
rating: {type: Number, "default": 0, min: 0, max: 5},
facilities: [String]
});
mongoose.model('Location', locationSchema);
/app_api/router/index.js
var express = require('express');
var router = express.Router();
var ctrlLocations = require('../controllers/locations');
/* Locations pages */
router.get('/locations/:locationid', ctrlLocations.locationsReadOne);
app_api/controllers/locations.js
var mongoose = require( 'mongoose' );
var location = mongoose.model('Location');
module.exports.locationsReadOne = function (req, res) {
if(req.params && req.params.locationid) {
var _id = mongoose.Types.ObjectId(req.params.locationid);
console.log(mongoose.Types.ObjectId.isValid(req.params.locationid)); // return true
location
.findById(req.params.locationid)
.exec(function(err, location) {
if (err) return res.status(500).send(err);
if (location) return res.status(200).json(location);
return res.status(404).json({"message":"locationid not found"});
});
} else {
return res.status(200).json({"message": "no location id in request"});
}
}
also tried with
findById(mongoose.Types.ObjectId(req.params.id)
find()
and findOne({"_id":_id)
but no results
Is there any bug in latest mongoose?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
node.js - mongoose doesn't return the output with findById
I was having a similar issue, so I updated my Model file. For e.g. const workoutsSchema = new Schema({ _id: mongoose.
Read more >Mongoose v6.8.2: API docs
Do not use this to create a new ObjectId instance, use mongoose. ... the MongoDB driver will try to find a server to...
Read more >How to use Populate in Mongoose & Node.js
In case of array of documents, if documents are not found, it will be an empty array. You can chain populate method for...
Read more >mongoose findbyid not working
Return Value¶ The collection.findOne() action returns a Promise that resolves to the first document in the collection that matches the query filter. If...
Read more >Find By ID in Mongoose - Mastering JS
In Mongoose, the Model.findById() function is used to find one document by its _id . The findById() function takes in a single parameter, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
mongoose working fine for me.
your codes are very much complicated. why dont you make it short and simple? Your are using MEAN right???