Mongoose数组索引不能正常工作

时间:2015-04-27 16:05:03

标签: node.js express mongoose

我有一个mongoose的文件,看起来像这样:

{  
  "_id":ObjectId("553e4d7a1ebb424364e47e0f"),
  "url":"/api/images/553e4d6f1ebb424364e47e0b",
  "type":"knowledge",
  "title":"asdasd",
  "week":18,
  "fields":{  
    "end":"sdajlsdjlfnsdf",
    "correctAnswer":"2",
    "answers":{  
      "0":"ljnsdlfjnsdfn",
      "1":"lnsldfnlsf",
      "2":"ljsndlfnsdlf"
    },
    "question":"sjlnasjld"
  },
  "participants":[  
    {  
      "user":ObjectId("553e4d531ebb424364e47e07"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e4e3bb4ad1bbb646a545c"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e4ecc007b12e464c6a7ff"),
      "result":{  
        "answer":"1"
      }
    },
    {  
      "user":ObjectId("553e4ecc007b12e464c6a7ff"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e5239b1d44ec7681bcb66"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e52be4d85843e69828df3"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e52cd8dded86469ee3974"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e537b6c4981b169a10c3d"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e5553afb1fd6e6a90e6af"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e55ddda5144976aa90224"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e563af09b82e46ac30c0c"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e563af09b82e46ac30c0c"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e563af09b82e46ac30c0c"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e57b6d554ab136caa95a8"),
      "result":{  
        "answer":"2"
      }
    },
    {  
      "user":ObjectId("553e5a7c094e51826d88dbff"),
      "result":{  
        "answer":"2"
      }
    }
  ]
}

我想要做的是限制'参与者'参与任务,如果他们已经完成了它。我正在使用此代码来尝试限制它们,但它不能正常工作:

if (mission.participants && mission.participants.map(function (e) { return e.user; }).indexOf(uid) > -1) {
  return res.send(403, { err: "You've already completed this mission" });
}

无论我尝试什么,indexOf函数始终返回-1

如果我运行console.log(mission.participants.map(function (e) { return e.user; }));,我会

[ 553e4d531ebb424364e47e07,
  553e4e3bb4ad1bbb646a545c,
  553e4ecc007b12e464c6a7ff,
  553e4ecc007b12e464c6a7ff,
  553e5239b1d44ec7681bcb66,
  553e52be4d85843e69828df3,
  553e52cd8dded86469ee3974,
  553e537b6c4981b169a10c3d,
  553e5553afb1fd6e6a90e6af,
  553e55ddda5144976aa90224,
  553e563af09b82e46ac30c0c,
  553e563af09b82e46ac30c0c,
  553e563af09b82e46ac30c0c,
  553e57b6d554ab136caa95a8,
  553e5a7c094e51826d88dbff ]

我已经确认uid变量实际上在数组中,所以这不是问题。

正在运行io.js v1.8.1mongoose v4.0.1MongoDB v2.6.9

1 个答案:

答案 0 :(得分:1)

问题是您要将对象Schema#ObjectIdString进行比较。您需要在地图函数中将ObjectId转换为String,以便indexOf可以找到所需的uid

if (mission.participants && mission.participants.map(function (e) { return e.user.toString(); }).indexOf(uid) > -1) {
  return res.send(403, { err: "You've already completed this mission" });
}