简体   繁体   English

从聊天框名称中查找用户ID时出错

[英]Error in finding user id from chatbox name

I am currently doing a project on online chatbox. 我目前正在在线聊天框上做一个项目。 However, i was stuck when I need to get the id of friends of the user by getting the name of the friend(eg. when i have Ivan in my friendList, I can get his id in the database) when logging in the server. 但是,当我需要通过获取朋友的名字来获取用户的朋友的ID时(例如,当我在朋友列表中有Ivan时,我可以在数据库中获取他的ID)时,我陷入了困境。 I can get the name of the friend, say, Ivan, but i tried many times and still dont know how to use the name i found to find the id of Ivan in the server. 我可以得到朋友的名字,例如Ivan,但是我尝试了很多次,但仍然不知道如何使用我在服务器中找到Ivan的ID的名字。 I am using mongodb and node.js to do this project. 我正在使用mongodb和node.js进行此项目。 Here is my current code: 这是我当前的代码:

router.get('/load', function(req, res)
var db = req.db;
var collection = db.get('userList')
if(req.session.userId)
{
 var id = req.session.userId;
 collection.find({"_id": id},{fields:{"_id":0, "friends.name":1}}, function(err, docs)
 res.json(docs);
 )}
 }

And here's what the database structure kind of looks like: 这是数据库结构的样子:

{
 "_id": ObjectId(5a23e06aa11f6589e02d8fcf),
 "name": "Ken",
 "password": "12345678",
 "friends":[
   {
     "name" : "Andy",
     "lastmsg": "0"
    },
    {
     "name" : "Ivan",
      "lastmsg" : "1"
     }
    ]
 }

Did you try 你试过了吗

collection.findOne({"friends.name": "Ivan"}, function(err, docs)
 res.json(docs);
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM