javascript - 根据请求设置对象。在调用第二个中间件时,两个中间件中的第一个似乎不存在

标签 javascript mongodb express mongoose

我有两个 express middlwares,其中一个将对象设置为 req,另一个使用该对象来打开 switch 语句。

这是一个插图:

module.exports = (req, res, next) => {
  if (!req.headers.authorization) {
    return res.status(401).end()
  }
  const token = req.headers.authorization.split(' ')[1]
  return jwt.verify(token, config.database.jwtSecret, (err, decoded) => {
    if (err) { return res.status(401).end() }

    const userId = decoded.sub
    return User.findById(userId, (userErr, user) => {
      if (userErr || !user) {
        return res.status(401).end()
      }
      req.user = {
        _id: user._id,
        name: user.name
      }
      return next()
    })
  })
}

    //My route
        userpage.get('/', authCheck, (req, res) => {
          return Event.findOne()
          .populate('attending.user', 'name') 
          .exec((err, newEvent) => {
            if (err) {
              console.log(err)
              res.status(400).end()
            }
            let uids = []
            let imAttending = false
            newDinner.attending.user.map(obj => {
              uids.push(obj._id)
              })
            console.log(uids) // Shows the array with all uids
            // Double checked that it is indeed an array
            let isArr = Object.prototype.toString.call(uids) === '[object Array]'
            console.log(isArr) // true
            console.log(req.user._id) // Shows the id and it's indeed matching one of the ids in the uids array
            imAttending = uids.indexOf(req.user._id) > -1
            console.log(imAttending)  // false <--- Should be true
            // Test
            let id = '57ec2203ba3e994c7c9d5832' // I litraly copy pasted that from the console.log(req.user._id)
            imAttendingII = uids.indexOf(id) > -1
            console.log(imAttendingII) // true ???? what the heck?
            // checking it's the same type as suggested in one of the comments
            let check = ['57ec2203ba3e994c7c9d5832'].indexOf(req.user._id) === -1
            console.log(check) //true
          })
        })

下面的评论让我放心,这不是一个异步问题,并且在我得到的结果之后,我对它可能是什么感到迷茫。

编辑: 以下内容虽然有效,但显示为真。但是即使在 _id 元素上执行 uids.indexOf(req.user._id.toString()) > -1 之后,检查 _id 字段也不起作用:

newEvent.attending.user.map(obj => {
      names.push(obj.name)
    })
    imAttending = names.indexOf(req.user.name) > -1 // imAttending = true

最佳答案

由于问题中提供的附加信息,决定添加另一个答案。

看起来您正在使用 MongoDB 和 Mongoose(如果我是对的,它们应该在标签中)。鉴于此,用户文档的 _id 属性将等于它的字符串形式,因为表示实际上是 ObjectID('blahblahblah'),它实际上不是引擎盖下的字符串。如果你 console.log() 它,它看起来像一个字符串,因为 console.log 在它的底层调用了 toString()。

所以你可能想尝试的评估是:

imAttending = uids.indexOf(req.user._id.toString()) > -1;
console.log(imAttending); // should be true

顺便说一句,这也是为什么使用类似 node-inspector 之类的东西来设置断点并单步执行代码而不是依赖控制台语句进行调试的重要原因。您将看到各个位的实际表示,而不是它们的字符串化形式。

关于javascript - 根据请求设置对象。在调用第二个中间件时,两个中间件中的第一个似乎不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39932829/

相关文章:

javascript - 对 mongoose 返回的结果使用 lodash 时输出不正确

php - 在 js .html() 方法中包含 <?php 标签?

javascript - 两个部分的 HTML 不同样式

mongodb - 如何找到数组mongodb的长度

mongodb - Doctrine MongoDB 结果缓存

postgresql - 如何使用 Sequelize 将 id 传递给其关联记录

javascript - 提交后重定向到另一个页面

javascript - 使用不断变化的 HashMap 和 Nodejs 写入 Excel

C# Mongo 查询效率

javascript - 从 Express、Node.js 应用程序中删除 MongoDB 文档