为什么 Mongoose 查询返回空数组
您好,我已经查看了与我提出的问题类似的问题,但没有一个解决方案适合我的问题。我正在开发一个API。
当我使用有效负载发送响应时,我从客户端获取请求有效负载,并在服务器端获取响应有效负载,当我尝试使用猫鼬查询(搜索)时,我遇到了空数组的问题。所有东西都签出,例如:
连接到猫鼬, 数据库确实存在, 集合存在并且具有我的接口/类的默认名称。
为了确保我有正确的数据库名称,我在第一次执行 mongoose.connect('mongodb://localhost/database
) 时创建了它
。我的 mongoose 架构文件存在。它采用默认值类上的名称,它是单数的,有人可以帮助告诉我我做错了什么吗,
Interface
export interface Products{
_id: string,
name: string
}
//imported in my service.ts
livesearch.js。 架构
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
name:{
type: String,
required: true
}
});
module.exports = mongoose.model('Products', productSchema, 'products');
product.js
router.post('/getProducts', async(req, res) =>{
let payload=req.body.payload;//server needs to extract payload property
console.log("Payload", payload);
console.log("Heading to search");
let search = await Products.find({name: {$regex: new RegExp('^'+payload+'.*',
'i')}}).exec();
console.log("Search", search) empty array
//Limit search to 10
search = search.slice(0, 10);
//res.send({payload:load}); //this works
res.send({payload:search});
})
数据库和集合
> show collections
productLiveSearch
products
>
Hello I have looked at similar questions to the one I asked but none of the resolutions fit my issue. I am developing an API.
I get the request payload from client and a response payload on the server side when I send response using payload, it's when I try to use a mongoose query(search) that I run into an issue with an empty array. All things checkout such as:
Connected to mongoose,
Database does exist,
Collection exist and has default name of my interface/class.
To be assured I had the correct db name I let it be created when I did mongoose.connect('mongodb://localhost/database
for first time
My mongoose schema file exists. It takes on the default name on the class and it is singular. I have included a small snippet. Can someone help tell me what I'm doing wrong,
Interface
export interface Products{
_id: string,
name: string
}
//imported in my service.ts
livesearch.js
Schema
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
name:{
type: String,
required: true
}
});
module.exports = mongoose.model('Products', productSchema, 'products');
product.js
router.post('/getProducts', async(req, res) =>{
let payload=req.body.payload;//server needs to extract payload property
console.log("Payload", payload);
console.log("Heading to search");
let search = await Products.find({name: {$regex: new RegExp('^'+payload+'.*',
'i')}}).exec();
console.log("Search", search) empty array
//Limit search to 10
search = search.slice(0, 10);
//res.send({payload:load}); //this works
res.send({payload:search});
})
Database and collection
> show collections
productLiveSearch
products
>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的收藏中可能没有任何东西;您需要首先填充它以便查询返回值。
Your collection likely doesn't have anything in it; you need to populate it first for a query to return with values.