-
-
Save timoxley/1314547 to your computer and use it in GitHub Desktop.
Mongoose JS Schema methods error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var express = require('express'); | |
var mongoose = require('mongoose'); | |
var fs = require('fs'); | |
var Schema = mongoose.Schema; | |
var db = mongoose.connect('mongodb://localhost/myapp'); | |
// Schemas | |
var UserSchema = new Schema({ | |
first_name:{type:String,required:true}, | |
last_name:{type:String,required:true}, | |
email:{type:String,required:true,index:{unique:true}}, | |
loc:{lng:Number,lat:Number} | |
}); | |
UserSchema.index({loc:'2d'}); | |
UserSchema.method('findNearbyMatches',function(lng,lat,cb) { | |
return this.find({loc:{$near:[lng,lat],$maxDistance:5}},cb); //<--- ERROR accessing "this", no find() method. SEE ERROR BELOW | |
}); | |
// Set up models | |
var User = mongoose.model('User',UserSchema); | |
app.get('/localmatches/:lng/:lat',requireAuth,function(req,res){ | |
var user = new User(); | |
user.findNearbyMatches(req.params.lng,req.params.lat,function(err,users){ | |
res.contentType('application/json'); | |
var result = {status:'ok',message:'',users:users}; | |
res.send(result); | |
}) | |
}); | |
/* | |
TypeError: Object #<model> has no method 'find' | |
at model.findNearbyMatches | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment