猫鼬findById即使具有有效ID也会返回null [英] Mongoose findById returns null even with valid id

查看:190
本文介绍了猫鼬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问题 https://github.com/Automattic/mongoose /issues/3079 不幸的是,我无法将猫鼬降级,因为它可能会影响其他多个工作功能. 编辑 : 我试过像这样创建ObjectId:

I went through this git issue as well https://github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆