我试图根据保存到MongoDB数据库中的用户与指定坐标的接近程度来查询它们,但是每次我使用Postman发出请求时,都会收到以下错误消息:
{
"message": "An unknown error occurred!"
}
这是我的用户架构:
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: { type: String, required: true },
geometry: {
type: {
type: String,
default: 'Point'
},
coordinates: {
type: [Number],
index: '2dsphere'
}
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model('User', userSchema);
这是我的控制器:
const { validationResult } = require('express-validator');
const HttpError = require('../models/http-error');
const User = require('../models/user');
const getUsers = async (req, res, next) => {
let users;
try {
users = await User.find({}, '-password');
} catch (err) {
const error = new HttpError('Fetching users failed, please try again later.', 500);
return next(error);
}
res.json({users: users.map(user => user.toObject({ getters: true }))});
};
const getUsersNearby = async (req, res, next) => {
let usersNearby;
try {
usersNearby = await User.geoNear({
near: [-93, 44],
maxDistance: 6,
}, '-password');
} catch (err) {
const error = new HttpError('Fetching users failed, please try again later.', 500);
return next(error);
}
res.json({usersNearby: usersNearby.map(userNearby => userNearby.toObject({ getters: true }))});
};
exports.getUsers = getUsers;
exports.getUsersNearby = getUsersNearby;
exports.signup = signup;
exports.login = login;
这是我的路线:
const express = require('express');
const { check } = require('express-validator');
const usersControllers = require('../controllers/users-controllers');
const router = express.Router();
router.get('/', usersControllers.getUsers);
router.get('/nearby', usersControllers.getUsersNearby);
router.post('/signup', [
check('name')
.not()
.isEmpty(),
check('geometry')
.not()
.isEmpty(),
check('email')
.normalizeEmail()
.isEmail(),
check('password').isLength({ min: 8 })
], usersControllers.signup);
router.post('/login', usersControllers.login);
module.exports = router;
最后是我的app.js文件:
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const database = require('./util/config/keys').mongoURI;
const usersRouters = require('./routes/user-routes');
const port = process.env.PORT || 5000;
const HttpError = require('./models/http-error');
const app = express();
// Middleware
app.use(bodyParser.json());
app.use('/api/users', usersRouters);
app.use((req, res, next) => {
const error = new HttpError('Could not find the specified route', 404);
throw error;
});
app.use((error, req, res, next) => {
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500);
res.json({ message: error.mesage || 'An unknown error occurred!' })
});
// Connect to MongoDB
mongoose.connect(database, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true })
.then(() => console.log('Successfully connected to your database!'))
.catch(err => console.log(err));
// Application listening on port 5000
app.listen(port, () => {
console.log(`Server up and running on port ${port}; ready for requests!`);
});
如果我在getUsersNearby
函数中将所有内容留空,那么我可以返回存储在数据库中的所有用户,这似乎是问题出在UserFind({..})
部分中的代码所在? >