环回中的相同 'find' 查询和 mongo 在不同服务器中具有不同的行为
Same 'find' query in loopback and mongo has different behaviors in different servers
我正在尝试在环回模型 JS 文件中进行查询。它非常简单,如下所示:
//isAdmin function: gets a boolean if a given userProfileId is an admin or not.
Userprofile.isAdmin = function(userProfileId, cb) {
let role = app.models.Role;
let rolemapping = app.models.RoleMapping;
let filter = {
"where": {
"name": "admin"
},
"fields": ["id"]
};
role.findOne(filter, function(err, instance) {
let filter2 = {
"where": {
"roleId": instance.id
,
"principalId": userProfileId,
},
"fields": ["id"]
};
rolemapping.find(filter2, function(err, instance) {
if (instance != "") {
cb(null, true);
} else {
cb(null, false);
}
});
});
};
这在我们的开发服务器中完美运行。 filter2
的console.log
,在那个服务器returns:
{ where:
{ roleId: 5890ef8bbef9b73e568c6933,
principalId: '5890ef8bbef9b73e568c6932' },
fields: [ 'id' ]
}
}
instance.id
的 console.log
看起来像:
ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 }
问题出在我们的生产服务器上。我已经做了部署过程,结果略有不同,但根本不起作用:(.
filter2
return的console.log
:
{ where:
{ roleId: 58921dff16d9a37009e21104,
principalId: '5890ef8bbef9b73e568c6932' },
fields: [ 'id' ] }
}
并且,console.log(instance.id)
returns:
ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] }
我们的生产服务器中的这个查询没有 return 任何文档,即使数据库中有满足查询的文档。
我比较了所有的npm包,都是完全一样的版本。还有服务器(我们使用的是 Debian 8)和 mongo (v3.2.10)。
有没有解决这个问题的想法?
提前致谢!
佩德罗。
您是否检查过 "userProfile".json 文件中的 userProfile id 类型?这似乎是一个 ids 类型的问题。您可以解决它,在您的 model.json 文件中添加类型为字符串的 id 属性,如下所示:
"properties": {
"userProfileId": {
"type": "string"
}
我在使用相关模型的 ID 进行查询时遇到了类似的问题。通过选择类似而不是等价来解决问题,如下所示:
{where:
{and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}},
{principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}]
}
},
{fields: [ 'id' ] }
另请注意,场投影应该是独立于 where 过滤器的对象。
我正在尝试在环回模型 JS 文件中进行查询。它非常简单,如下所示:
//isAdmin function: gets a boolean if a given userProfileId is an admin or not.
Userprofile.isAdmin = function(userProfileId, cb) {
let role = app.models.Role;
let rolemapping = app.models.RoleMapping;
let filter = {
"where": {
"name": "admin"
},
"fields": ["id"]
};
role.findOne(filter, function(err, instance) {
let filter2 = {
"where": {
"roleId": instance.id
,
"principalId": userProfileId,
},
"fields": ["id"]
};
rolemapping.find(filter2, function(err, instance) {
if (instance != "") {
cb(null, true);
} else {
cb(null, false);
}
});
});
};
这在我们的开发服务器中完美运行。 filter2
的console.log
,在那个服务器returns:
{ where:
{ roleId: 5890ef8bbef9b73e568c6933,
principalId: '5890ef8bbef9b73e568c6932' },
fields: [ 'id' ]
}
}
instance.id
的 console.log
看起来像:
ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 }
问题出在我们的生产服务器上。我已经做了部署过程,结果略有不同,但根本不起作用:(.
filter2
return的console.log
:
{ where:
{ roleId: 58921dff16d9a37009e21104,
principalId: '5890ef8bbef9b73e568c6932' },
fields: [ 'id' ] }
}
并且,console.log(instance.id)
returns:
ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] }
我们的生产服务器中的这个查询没有 return 任何文档,即使数据库中有满足查询的文档。
我比较了所有的npm包,都是完全一样的版本。还有服务器(我们使用的是 Debian 8)和 mongo (v3.2.10)。
有没有解决这个问题的想法?
提前致谢!
佩德罗。
您是否检查过 "userProfile".json 文件中的 userProfile id 类型?这似乎是一个 ids 类型的问题。您可以解决它,在您的 model.json 文件中添加类型为字符串的 id 属性,如下所示:
"properties": {
"userProfileId": {
"type": "string"
}
我在使用相关模型的 ID 进行查询时遇到了类似的问题。通过选择类似而不是等价来解决问题,如下所示:
{where:
{and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}},
{principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}]
}
},
{fields: [ 'id' ] }
另请注意,场投影应该是独立于 where 过滤器的对象。