技术堆栈:React,Node和MongoDB(本机驱动程序)。
我正在使用一个简单的MongoDB findOne方法来通过电子邮件查找用户。在此findOne方法中,我包括了投影字段,该字段应该从数据库中的MongoDB文档中排除或包括某些字段。
响应将发送到一个回调函数,该函数会将用户注销(我将演示)。
我的问题:当我使用此投影额外参数时,我所做的任何事情都不会影响我从请求中得到的结果。我是缺少什么还是做错了吗?
代码:
MongoDB方法:
static findUserInDatabase = ( method, searchParam, callback ) => {
const db = getDb();
if ( method === "email" ) {
//This is where I have the issue
db.collection( process.env.USERSCOLLECTION ).findOne( { email: searchParam.trim() }, { password: 0 } ).then( r => callback( r) ).catch( e => callback( e ) );
} else {
db.collection( process.env.USERSCOLLECTION ).findOne( { tokens: searchParam.trim() } ).then( r => callback( r ) ).catch( e => callback( e ) );
}
};
记录用户的节点代码:
User.findUserInDatabase( "email", friendEmail.email, ( friend ) => {
console.log( friend );
//Inside of this console.log I get all of the fields of the user, such as password field.
} );
以下是MongoDB文档:
“以下操作将在bios集合中找到一个文档,并且仅返回名称,贡献和_id字段:”
db.bios.findOne(
{ //Query },
{ name: 1, contribs: 1 }
)