javascript - 在嵌套对象中读取嵌套对象
问题描述
我的问题是读取嵌套对象的属性,该对象位于其他嵌套对象内。
GraphQL
type Mapping {
id: ID!
partnerSegmentId: ID!
ctSegmentId: CtSegment!
}
type PartnerSegment {
id: ID!
name: String!
platformId: Int!
partner: Partner!
}
type Partner {
id: ID!
name: String!
}
一旦我尝试像这样查询它:
{
allMappings {
partnerSegmentId {
id
name
partner {
id
}
}
}
}
我收到:
{
"data": {
"allMappings": [
null
]
},
"errors": [
{
"message": "Cannot return null for non-nullable field Partner.name.",
"locations": [
{
"line": 8,
"column": 9
}
],
"path": [
"allMappings",
0,
"partnerSegmentId",
"partner",
"name"
]
}
]
}
映射架构
const mappingSchema = new mongoose.Schema(
{
partnerSegmentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'PartnerSegment',
required: [true, 'Mapping must have partner segment id.']
},
ctSegmentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'CtSegment',
required: [true, 'Mapping must have CT segment id.']
}
},
{ timestamps: true }
);
我尝试分别阅读 Partner、PartnerSegment 和 Mapping 模型。一切正常。知道我应该在哪里搜索问题的根源吗?我检查了 mongodb 文档,ids 看起来还不错。我想这是我的模型的错。
如果你想仔细看看它的项目 repo。
解决方案
解决方案:
返回值中的垃圾 ID 是由嵌套实体中的填充无效引起的。我设法解决问题的方式:
const allMappings = () =>
Mapping.find({})
.populate('user')
.populate('ctSegment')
.populate({
path: 'partnerSegment',
populate: {
path: 'partner'
}
})
.exec();
推荐阅读
- haskell - 当非单子值变成单子值时正确重构 Haskell 代码
- node.js - 需要 CLIENTID 的 OAuth2Strategy 出错
- angular6 - 如何以角度使用 MapMyIndia API?
- php - Symfony 4 表单类型翻译
- arduino - ATMEGA32u4 Arduino 引导加载程序问题
- xpath - knime xpath 节点多标签选择
- swift - 如何在展示 adMob 横幅广告时提高 SpriteKit 游戏的性能?
- css - 如何让我的 CSS 过渡在 iPad (iOS) 上工作?
- node.js - 在 aws lambda 中使用 nodejs 在 s3 存储桶中上传多个图像?
- go - 使用通道或互斥体模拟无线电通信