首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CastError:在路径"_id“处转换为ObjectId失败

CastError:在路径"_id“处转换为ObjectId失败
EN

Stack Overflow用户
提问于 2018-07-30 00:30:36
回答 2查看 4.1K关注 0票数 2

我知道有很多这样的问题,但从我的尝试来看,似乎没有什么是可行的。

快速概述应用程序nodejs后端,它使用365个护照验证用户,然后在ReactJS前端使用。

我在udemy上使用课程跟踪Node,直到我开始收到以下错误,它才开始工作:

“在”"00037ffe-0944-74f2-0000-000000000000@84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa“”路径下"_id“为”_id“表示"office”的值转换为ObjectId失败

我是MongoDB和猫鼬的新手,所以我真的不知道该看什么。我想它在我的passport.use第四节的某个地方。

一旦我重新构建了MongoDB和所有与它相关的内容(基本模式等等)错误就会消失。(我指的是与新的mongo集合/实例完全相同的代码。)

然后,我得到365护照认证工作,但一段时间后,同样的错误将显示出来。如果你需要更多的片段,请告诉我,这是我到目前为止的情况:

以下是我的护照Outlook策略:

代码语言:javascript
运行
AI代码解释
复制
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);
          }

        }
      )
    );

Serialize/Deserialize:

代码语言:javascript
运行
AI代码解释
复制
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);
  });
});

My:

代码语言:javascript
运行
AI代码解释
复制
const mongoose = require("mongoose");
const { Schema } = mongoose;

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

最后是我的身份验证路由:

代码语言:javascript
运行
AI代码解释
复制
 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");
    }
  );
};

我知道这不仅仅是必要的,但我宁愿现在尽可能多地付出。

如果你有修理/改进,请寄给我。

任何帮助都将不胜感激。耽误您时间,实在对不起。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-07 02:20:08

这个帮我修好了:

代码语言:javascript
运行
AI代码解释
复制
passport.deserializeUser((id, done) => {
  Office.findById(id).then(user => {
    done(null, user);
  });

});
票数 0
EN

Stack Overflow用户

发布于 2018-07-30 01:21:41

尝试更改下面的代码

当前:

代码语言:javascript
运行
AI代码解释
复制
passport.deserializeUser((id, done) => {
  //turns id into user (mongoose model instance)
  Office.findById(id).then(user => {
    done(null, user);
  });
});

期望的

代码语言:javascript
运行
AI代码解释
复制
passport.deserializeUser((id, done) => {
  //turns id into user (mongoose model instance)
  Office.findById(mongoose.Types.ObjectId(id)).then(user => {
    done(null, user);
  });
});

我觉得这应该管用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51589887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档