Node.js 为什么find方法在Mongoose中不起作用
我正在与猫鼬一起开发一个express应用程序Node.js 为什么find方法在Mongoose中不起作用,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在与猫鼬一起开发一个express应用程序 .find方法在其他路径中工作,但在我的博客路径中,它不工作 这是我的猫鼬模型: const mongoose = require('mongoose'); let BlogSchema = new mongoose.Schema({ title: {type: String, unique: true, required: true}, body: {type: String}, category: Strin
.find
方法在其他路径中工作,但在我的博客路径中,它不工作
这是我的猫鼬模型:
const mongoose = require('mongoose');
let BlogSchema = new mongoose.Schema({
title: {type: String, unique: true, required: true},
body: {type: String},
category: String,
permalink: String,
date: {type: Date, default: Date.now},
image: {type: String},
imageId: String,
tags:[{type: String}],
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
let Blog = mongoose.model('Blog', BlogSchema);
module.exports = Blog;
这是我的路由器。获取:
//SHOW ROUTE
router.get('/blog/:category/:permalink', (req,res)=>{
console.log(req.params) //{ category: 'travel', permalink: 'why-travel-to-places' }
Blog.find({category: req.params.category,permalink: req.params.permalink}, (err, foundPost)=> {
if (err){
console.log(err)
} else {
console.log(foundPost) // []
res.render('blog/show', {post: foundPost});
}
});
});
如您所见,Iconsole.log
req.params
和foundPost
。
req.params结果是{category:'travel',permalink:'why travel to el nido'}
,而foundPost是一个空数组[]
现在我使用db.blogs.find({category:“travel”}).pretty()
查看了mongo,它找到了它。但在我的表演路线中,它没有找到它
我查阅了与我的问题相关的其他问题,有人说可能它与模式有问题,但在我的例子中,它不是
my.find()
正在其他路由中工作,但在此路由中不工作
我错过什么了吗,请帮忙。谢谢我非常确定.find()
工作正常,但在数据库中找不到任何具有该查询条件的文档。请注意,在路线代码中,您正在查询{category:'travel',permalink:'why travel to el nido'}
,即匹配的博客文档必须具有category:'travel'
和permalink:'why travel to'字段,但当您使用db.blogs.find查看Mongo时({category:'travel}).pretty()
请使用db.blogs.find({category:“travel”,permalink:“why travel to el nido”}).pretty()
在Mongo中再次搜索,以模拟来自路线的相同的查询,并查看是否可以找到任何符合该条件的文档
我猜您不会找到任何,因此mongoose.find()
返回一个空数组,意思是“没有匹配项”
编辑:我刚看到来自的评论,可能mongoose配置没有指向正确的数据库。按照他的指示,确保您正在查询所需的集合。您确定您指向的是正确的数据库名称空间,甚至是服务器吗?您正在正确地查找博客的集合名称,因为mongoose就是这么做的,但请确保您的应用程序连接字符串指向您认为它所在的位置。记住当您打开mongo
shell时,数据库是test
。同时启用调试mongoose.set('debug',true)
。在MongoDB。我的永久链接在永久链接内有?
。但是在猫鼬里面。没有?
。非常感谢你说得对,我没有找到任何关于db.blogs.find({category:“travel”,permalink:“why travel to el nido”}).pretty()
。谢谢你指出这一点。谢谢你,事实证明。在MongoDB中,我的permalink在permalink中有?
。但是在猫鼬里面。没有?
。非常感谢你。它正在工作