【问题标题】:Mongoose nested aliasMongoose 嵌套别名
【发布时间】:2021-07-09 16:57:06
【问题描述】:

我在 MongoDB 中有以下字段,我无法更改 DB 的架构

phoneNumbers: [
    { phoneNumber: '15141234567', type: 'WORK' },
    { phoneNumber: '15142233667', type: 'CELL' },
    { phoneNumber: '1517654321', type: 'HOME' }
  ],

架构是

const schemaPhoneNumber = new Schema({
  type: {
    type: String,
    enum: ["WORK", "CELL", "HOME"],
    alias: "phoneType",  /////////////////////////////////// I have alias the field name
  },
  phoneNumber: String,
});

const GraphQLPhoneType = new GraphQLEnumType({
  name: "PhoneType",
  values: {
    WORK: {
      value: "WORK",
    },
    CELL: {
      value: "CELL",
    },
    HOME: {
      value: "HOME",
    },
  },
});
const GraphQLPhoneNumber = new GraphQLObjectType({
  name: "PhoneNumber",
  fields: () => ({
    phoneType: { ////////////////////////////////////// it is not finding by this name
      type: GraphQLNonNull(GraphQLPhoneType),
    },
    phoneNumber: {
      type: GraphQLNonNull(GraphQLString),
    },
  }),
});

问题:当我运行查询时,它显示“phoneNumber”但“phoneType”返回 null。

如果我将“phoneType”更改为“type”,查询会显示正确的类型(WORK、CELL 等),但我必须使用“phoneType” " 在 graphQL 中,"type" 是 DB。

问题是什么,当我通过别名进行更改时,它在“GraphQLPhoneNumber”的字段中不可用,是嵌套 JSON 的问题还是我应该以不同的方式调用别名?

【问题讨论】:

    标签: mongoose graphql mongoose-schema


    【解决方案1】:

    别名是正确的,问题是我创建上层架构时没有调用schemaPhoneNumber。

    phoneNumbers: [schemaPhoneNumber],
    

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2020-11-04
      • 2014-02-07
      • 2011-01-22
      • 2017-07-06
      • 1970-01-01
      • 2013-11-06
      • 2016-04-25
      • 1970-01-01
      相关资源
      最近更新 更多