猫鼬findById即使具有有效ID也会返回null

编程入门 行业动态 更新时间:2024-10-25 08:27:06
本文介绍了猫鼬findById即使具有有效ID也会返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经看过有关以下标题类似的问题的讨论 猫鼬'findById'返回具有有效ID的null 但是我的问题不是数据库名称,因为我与同一个数据库的所有其他连接实际上在同一个集合上的查询都可以正常工作. 我正在使用猫鼬4.13.6,节点js 6.11和mongo 3.4. 这是一个发帖请求.

I have already seen the discussion about the following question with a similar title mongoose 'findById' returns null with valid id But my problem is not the database name since all my other connections with the same database in fact the queries on the same collection are working fine. I am using mongoose 4.13.6, node js 6.11 and mongo 3.4. It is a post request .

var query=req.body;

我将搜索参数发送为

var findFruit = { _id:query._id }

当我打印findFruit时,我得到:

When I print my findFruit I get :

_id:'5a1cf77920701c1f0aafb85e'

为此的控制器功能是:

Fruit.findById(findFruit._id,function(err,fruit){ if( _.isNull(err) ){ var response = genRes.generateResponse(true,"found successfully"); callback(response); } else{ var response = genRes.generateResponse(false,"there occured some error : "+err); callback(response); } })

我什至试图找到

Fruit.find(findFruit,function(err,fruit){ if( _.isNull(err) ){ var response = genRes.generateResponse(true,"found successfully"); callback(response); } else{ var response = genRes.generateResponse(false,"there occured some error : "+err); callback(response); } })

确保该集合的条目在此ID下.

The collection for sure has the entry under this id .

我也遇到了这个git问题 github/Automattic/mongoose /issues/3079 不幸的是,我无法将猫鼬降级,因为它可能会影响其他多个工作功能. 编辑 : 我试过像这样创建ObjectId:

I went through this git issue as well github/Automattic/mongoose/issues/3079 Unfortunately I cannot downgrade mongoose as it might affect multiple other working functions. Edit : I tried creating ObjectId like :

var mongoose = require('mongoose'); var ObjectID = require('mongodb').ObjectID; var objectId = new ObjectID(); // Convert the object id to a hex string var originalHex = objectId.toHexString(); // Create a new ObjectID using the createFromHexString function var newObjectId = new ObjectID.createFromHexString(query._id);

模型文件:

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; var FruitSchema = new Schema({ name : {type : String, unique : true}, color : {type : String} }); module.exports = mongoose.model('Fruit', FruitSchema);

推荐答案

仍然试图弄清楚为什么findById对我不起作用,但是下面的代码可以做到这一点

Still trying to figure out why findById didn't work for me but the following piece of code did it

var mongoose = require('mongoose'); var newObjectId=new mongoose.Types.ObjectId(query._id); var params={ '_id':newObjectId } Fruit.find(params).exec(function (err,fruit) { if( _.isNull(err) ){ var response = genRes.generateResponse(true,"found successfully"); callback(response); } else{ var response = genRes.generateResponse(false,"there occured some error : "+err); callback(response); } })

更多推荐

猫鼬findById即使具有有效ID也会返回null

本文发布于:2023-11-08 11:37:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   findById   猫鼬   null   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!