无法使用节点js / mongodb / socket.io从数据库中删除

时间:2016-04-28 19:15:26

标签: node.js mongodb socket.io

这是我的数据库表'users'

的架构
{
   "_id": {
    "$oid": "572255b0dad7d9781f92d6bd"
   },
   "local": {
       "password": "$2a$08$JMHr7CMBNkFqi1xxFvO9je1w9qi2BP4tR9Z81FsA2N267PNIBD3ma",
       "email": "a@a.a"
   },
   "__v": 0
}

我正在尝试使用以下代码从我的数据库中删除它: functions.js:

function deleteUser(finishedEmail){
   try{
     var socket = io.connect('http://127.0.0.1:8080');
     console.log("success");
   } catch(e){
     console.log("fail");
   }
   if(socket !== undefined){
       var email = finishedEmail;
       socket.emit('userDelete',{
         email:email
       })
   }
}

server.js:

    var col2 = db.collection('users');
    socket.on('userDelete',function(data){
        var email = data.email;
        console.log(data.email);
        col2.deleteOne({email: email}, function(){
            console.log("successful deletion");
        });
    });

在我的控制台中,我收到了日志:“a@a.a”和“成功删除”。但电子邮件不会被删除。我做错了什么?

我尝试了以下内容:

col2.deleteOne({local:{email: email}}, function(){
    console.log("successful deletion");
});

col2.deleteOne({email: email}, function(){
    console.log("successful deletion");
});

col2.deleteOne({local.email: email}, function(){
    console.log("successful deletion");
});

1 个答案:

答案 0 :(得分:1)

我认为在你的server.js中你必须写 -

col2.deleteOne({"local.email": email}, function(){
   console.log("successful deletion");
});