Javascript 查找函数在具有指定字段的mongoose中不起作用
所以我正在制作这个网站,在那里你可以看到不同的食物食谱。在浏览菜谱时,我希望用户能够选择一个类别,并浏览该类别下的所有菜肴(即甜点、晚餐、素食主义者等) 我在路由器文件中创建了一个函数,返回指定类别下的所有菜肴:Javascript 查找函数在具有指定字段的mongoose中不起作用,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,所以我正在制作这个网站,在那里你可以看到不同的食物食谱。在浏览菜谱时,我希望用户能够选择一个类别,并浏览该类别下的所有菜肴(即甜点、晚餐、素食主义者等) 我在路由器文件中创建了一个函数,返回指定类别下的所有菜肴: router.get('/showrecipes/:categoryname', (req, res, next) => { let nameQuery = {category: req.params.categoryname}; Recipe.find(na
router.get('/showrecipes/:categoryname', (req, res, next) => {
let nameQuery = {category: req.params.categoryname};
Recipe.find(nameQuery, (err, recipes) => {
if (err) throw err;
res.json(recipes);
});
});
然而,当我试图在《邮递员》中测试它时,我总是得到空值,而不是所有属于该类别的菜肴。
我的所有其他功能都正常工作,但这一个似乎有问题
以下是RecipeOuter文件的其余部分供参考:
const express = require('express');
const passport = require('passport');
const Recipe = require('../models/recipe');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
router = express.Router();
router.get('/', (req, res, next) => {
res.json('Here are the recipes!')
});
// Is working
router.get('/showrecipes', (req, res, next) => {
Recipe.find({}, (err, recipes) => {
if (err) throw err;
res.json(recipes);
});
});
// Is working.
router.get("/showrecipes/:recipename", (req, res, next) => {
let nameQuery = {name: req.params.recipename};
Recipe.findOne(nameQuery, (err, recipes) => {
if (err) throw err;
res.json(recipes);
})
});
// Is not crashing, but is returning Null which isn't how it's supposed to work.
router.get('/showrecipes/:categoryname', (req, res, next) => {
let nameQuery = {category: req.params.categoryname};
Recipe.find(nameQuery, (err, recipes) => {
if (err) throw err;
res.json(recipes);
});
});
// Now it's working, good stuff.
router.post('/addrecipe', (req, res, next) => {
Recipe.create({
name: req.body.name,
description: req.body.description,
steps: req.body.steps,
ingredients: req.body.ingredients,
category: req.body.category,
}, (err, recipe) => {
if (err) throw err;
// Recipe.save();
res.json(recipe);
});
});
// See if this works
router.put('editrecipe/:recipename/:editedField', (req, res, next) => {
Recipe.findOneAndUpdate({name: req.params.recipename}, {$set: req.body}, {new: true}, (err, recipe) => {
if (err) throw err;
res.json(recipe)
});
});
// It's working, thank god
router.delete('/deleterecipe/:recipename', (req, res, next) => {
let nameQuery = {name: req.params.recipename};
Recipe.findOneAndRemove(nameQuery, (err, recipe) => {
if (err) throw err;
res.send('Dish was succesfully deleted!')
});
});
module.exports = router;
这是我的app.js文件
let express = require('express');
let mongoose = require('mongoose');
let path = require('path');
let bodyParser = require('body-parser');
let recipeRouter = require('./routes/recipeRouter');
let userRouter = require('./routes/userRouter');
let bcrypt = require('bcrypt');
let passport = require('passport');
let LocalStrategy = require('passport-local').Strategy;
let config = require('./config');
mongoose.connect(config.mongoUrl);
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
});
const app = express();
const port = 3000;
app.listen(port);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'views'));
app.use('/users', userRouter);
app.use('/recipes',recipeRouter);
app.get('/', function(req, res){
res.send('Hey, this is your database!')
});
module.exports = app;
这是我的配方文件
const express = require('express');
const mongoose = require('mongoose');
const User = require('../models/user');
let Schema = mongoose.Schema;
let commentSchema = Schema({
rating: {
type: Number,
required: true,
min: 1,
max: 5,
},
comment: {
type: String,
required: true
},
postedBy: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});
let Comment = mongoose.model('Comment', commentSchema);
let recipeSchema = Schema({
name: {
type: String,
required: true
},
description: {
type: String,
},
steps: {
type: String,
required: true,
},
ingredients: {
type: Array,
required: true
},
comments: [commentSchema],
category: {
type: String,
required: true,
},
postedBy: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});
/// So I learnt that by defining the string as "Recipe" in the model function, I will have to lower case it
/// and pluralize it when I use it with res.json and other such things (i.e. "Recipe" => recipes).
let Recipe = mongoose.model('Recipe', recipeSchema);
module.exports = Recipe;
/// refactor this so that these are in the router, not in the models file
/*
module.exports.getRecipeByName = (name, callback) => {
let nameQuery = {name: name};
Recipe.findOne(nameQuery, callback);
};
module.exports.getRecipesByCategory = (category, callback) => {
Recipe.find({'category': category});
};
*/
代码似乎没有问题,只是想指出通常
null
是err
包含的值,并且您的参数顺序似乎正确。也许这会有助于追踪原因。你能粘贴显示你在哪里初始化配方的代码吗?我发布了其余的代码,如果它有帮助的话。你应该从回调中删除next
参数,因为这使它们像中间件一样工作,而不是路由处理程序。否则代码看起来很好。