CastError:投放到ObjectId的路径“ _id”失败

时间:2018-07-30 08:30:36

标签: node.js mongodb reactjs mongoose mongodb-query

我知道这里有多个问题,但是根据我的尝试,似乎没有任何效果。

该应用程序的快速概览,nodejs后端通过365护照身份验证对用户进行身份验证,然后在ReactJS前端中使用它。

我在udemy上的Node上进行了React的全栈Web开发课程的学习,直到我开始收到以下错误,它一直有效:

  

“投射到ObjectId的值失败   “ 00037ffe-0944-74f2-0000-000000000000 @ 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaaaa”   在模型“办公室”“

的路径“ _id”处

我是MongoDB和Mongoose的新手,所以我真的不知道要看什么。我认为它在我的password.use auth部分中。

一旦我重新构建了MongoDB并附带了所有内容(基本架构等),错误就会消失。 (而且我的意思是与新的mongo集合/实例完全相同的代码。)

然后我获得了365护照认证,但是一段时间后,同样的错误也会显示出来。如果您需要更多片段,请告诉我,这是到目前为止的内容:

这是我的护照 Outlook策略:

passport.use(
      new OutlookStrategy(
        {
          clientID: keys.OUTLOOK_CLIENT_ID,
          clientSecret: keys.OUTLOOK_SECRET,
          callbackURL: "/authorize-outlook"
        },
        async (accessToken, refreshToken, profile, done) => {
          const exist = await Office.findOne({ outlookID: profile.id }).catch(
            error => console.log("error: ", error)
          );
          if (exist) {
            return done(null, exist);
          } else {
            const user = new Office({
              outlookID: profile.id,
              displayName: profile.displayName
            }).save();
            done(null, user);
          }

        }
      )
    );

序列化/反序列化:

passport.serializeUser((user, done) => {
  //turns user into id
  // see mlabs _id, identifying ID added my mongo
  // this is not profile id from google
  done(null, user.id);
});

// ID into a mongoose instance
// id placed in cookie above in "serializeUser" i.e the user.id
passport.deserializeUser((id, done) => {
  //turns id into user (mongoose model instance)
  Office.findById(id).then(user => {
    done(null, user);
  });
});

我的架构:

const mongoose = require("mongoose");
const { Schema } = mongoose;

OfficeSchema = new Schema({
  outlookID: String,
  displayName: String
});
mongoose.model("office", OfficeSchema);

最后我的身份验证路由:

 app.get(
    "/auth/outlook",
    passport.authenticate("windowslive", {
      scope: [
        "openid",
        "profile",
        "offline_access",
        "https://outlook.office.com/Mail.Read"
      ]
    })
  );

  //   Use passport.authenticate() as route middleware to authenticate the
  //   request.  If authentication fails, the user will be redirected back to the
  //   login page.  Otherwise, the primary route function will be called,
  //   which, in this example, will redirect the user to the home page.
  app.get(
    "/authorize-outlook",
    passport.authenticate("windowslive"),
    (req, res) => {
      res.redirect("/dashboard");
    }
  );
};

我知道这不仅仅是必要的,但我现在宁愿提供尽可能多的东西。

如果您有修复/改进,请发送给我。

任何帮助将不胜感激。谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

尝试更改以下代码

当前:

passport.deserializeUser((id, done) => {
  //turns id into user (mongoose model instance)
  Office.findById(id).then(user => {
    done(null, user);
  });
});

预期

passport.deserializeUser((id, done) => {
  //turns id into user (mongoose model instance)
  Office.findById(mongoose.Types.ObjectId(id)).then(user => {
    done(null, user);
  });
});

我认为这应该有效

答案 1 :(得分:0)

这为我修复了此问题:

passport.deserializeUser((id, done) => {
  Office.findById(id).then(user => {
    done(null, user);
  });

});