【问题标题】:Cast to ObjectId failed for value \"{ proposser: '618e49a68e18cd48286de4b5' }\" (type Object) at path \"_id\" for model \"Proposal\"对于模型 \"Proposal\" 的路径 \"_id\" 处的值 \"{ proposser: '618e49a68e18cd48286de4b5' }\" (type Object) 转换为 ObjectId 失败
【发布时间】:2021-12-25 08:40:53
【问题描述】:

我在 graphql 操场上遇到这个错误(下图)。 我也在解析器中检查了 objectId 的有效性。

// 型号

 const ProposalSchema = new Schema({
      cover
    Letter: {
        type: String,
      },
      budget: {
        type: String,
      },
      proposals: {
        type: mongoose.Schema.Types.ObjectId,
      },
      _id: {
        type: mongoose.Schema.Types.ObjectId,
      },
    });

//解析器 还检查了如果使用 mongoose.isValidObjectId(proposser) 的参数有效,则返回 true

Query: {
    proposals(_, args) {
      const { proposser } = args;
      return Proposal.findById({
        proposser,
      });
    },
  },

// 架构

const typeDefs = gql`
  type Proposal {
    _id: ID!
    coverLetter: String
    budget: String
    proposser: ID!
  }
`;

const Proposal = mongoose.model("Proposal", ProposalSchema);

【问题讨论】:

    标签: graphql backend apollo


    【解决方案1】:

    我在解析器中使用了错误的方法。 findById 被用于字段非 Id 字段。

     async proposals(_, args) {
      const { proposser } = args;
      const userProposals = await Proposal.find({
        proposser,
      });
    
      try {
        const result = userProposals;
        return result ? result : [];
      } catch (err) {
        console.log(err);
      }
    },
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 2019-06-26
      • 2020-05-11
      • 2023-03-28
      • 2013-06-17
      • 2020-10-19
      • 1970-01-01
      • 2017-05-23
      • 2018-07-10
      相关资源
      最近更新 更多