I have this route defined, but any requests made to it get's stuck on 'pending' and runs forever.
When I log the code, I see 1
followed by 4
, which means the code inside the find method never gets executed
# Calendar routes
router.get '/calendars', (req, res) ->
console.log '1'
Calendar.find (err, calendars) ->
console.log "2" + err
console.log "3" + calendars
res.send(err) if err
res.json(calendars)
return
console.log '4'
return
Model
mongoose = require("mongoose")
module.exports = mongoose.model("Calendar",
name: String
)
Any ideas on why this is?
The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).
What is find() in Mongoose? Mongoose's find() method is a query to retrieve a document or documents that match a particular filter.
and it will return array? find returns an array always.
The simple and easy way is to use the $in operator. The $in operator takes an array as its value. And $in operator, in turn, is assigned to the field according to which the filtration is to be done. Let's use the $in operator.
Until you call mongoose.connect
, your mongoose queries will simply be queued up.
Add code like this in your startup code to connect:
mongoose.connect('mongodb://localhost/test', function(err) {
if (err) {
console.error(err);
} else {
console.log('Connected');
}
});
In the connection string, replace test
with the name of your database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With