我正在使用nodejs,猫鼬和graphql,并且我的数据库中具有geojson 2D坐标,但是我无法通过graphql查询它们,它总是返回null
我尝试使用https://github.com/ghengeveld/graphql-geojson中的PointObject
模式来替换我的Geometry
模式,但存在相同问题
我的代码:
const Geometry = new GraphQLObjectType({
name: 'Geometry',
fields: () => ({
type: {
type: GraphQLString
},
coordinates: {
type: new GraphQLList(GraphQLFloat)
},
}),
})
const AntenneType = new GraphQLObjectType({
name: 'AntenneType',
fields: () => ({
_id: {
type: GraphQLID
},
type: {
type: GraphQLString
},
geometry: {
type: Geometry
},
properties: {
type: Properties
}
}),
});
const query = new GraphQLObjectType({
name: 'Query',
fields: {
antennes: {
type: new GraphQLList(AntenneType),
resolve: (obj, args, context, info) => {
return Antenne.find().limit(2) //Antenne is my mongoose model that return same data as in the data.json file
.then(antennes => {
console.log(antennes)
return antennes
})
}
}
},
});
一组数据:
{
"properties": {
...
},
"geometry": {
"type": "Point",
"coordinates": [
2.231666666667,
49.223611111111
]
},
"_id": "5cf1901b228293201fe248dc",
"type": "Feature"
}
我的GraphQl查询:
query{
antennes{
_id
properties{
generation
adm_lb_nom
}
geometry{
coordinates
}
}
}
结果:
{
"data": {
"antennes": [
{
"_id": "5cf1901b228293201fe248dc",
"properties": {
"generation": "2G",
"adm_lb_nom": "SFR"
},
"geometry": {
"coordinates": null
}
}
]
}
}
我还对我的全部架构和数据做了精要:https://gist.github.com/yadPe/cb397175a8c39021d0dab2208fe22a4d
我的猫鼬模式(根据@DanielRearden答案编辑):
const geoSchema = new Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
});
const antenneSchema = new Schema({
type: String,
properties: {
sup_id: Number,
tpo_id: Number,
sta_nm_dpt: String,
adr_nm_cp: Number,
generation: String,
coordonnees: String,
sup_nm_haut: Number,
adm_lb_nom: String,
emr_lb_systeme: String,
coord: String,
emr_dt_service: String,
date_maj: String,
code_insee: String,
nat_id: Number,
_id: Number,
com_cd_insee: String,
id: Number,
total_de_adm_lb_nom: String,
sta_nm_anfr: String
},
geometry: {
geoSchema
}
}, { collection: '2G_France' });
module.exports = mongoose.model('Antenne', antenneSchema);
我对猫鼬返回的数据做了一些控制台记录:
Antenne.find().limit(1)
.then(antennes => {
//return antennes
return antennes.map(antenne => {
console.log(antenne.geometry)
console.log(typeof antenne.geometry)
console.log(antenne.geometry.type)
console.log(antenne.geometry.coordinates)
const test = JSON.parse(JSON.stringify(antenne.geometry)) // idk why I need to do that here
console.log(test.type)
console.log(test.coordinates)
return antenne
})
});
并得到以下结果:
{ type: 'Point',
coordinates: [ 2.323333333333, 48.346666666667 ] }
object
undefined
undefined
Point
[ 2.323333333333, 48.346666666667 ]
答案 0 :(得分:0)
docs显示了这样定义的点模式:
@ViewChild("myTable") myTable:MatTable;
list() {
dataSource1 = .....
myTable.renderRows();
}
文档中的示例特别警告不要将其定义为const geoSchema = new mongoose.Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
});
。我怀疑这样做(就像您当前的代码一样)会导致整个子文档被序列化为String。这将解释您所看到的内容,因为您仍然可以将整个子文档打印到控制台。 GraphQL会将{ type: String }
字段解析为一个字符串,该字符串在JavaScript中从技术上讲是一个对象。但是,它无法解析geometry
或coordinates
字段,因为String上不存在这些属性,导致这些字段解析为null。
修复猫鼬模式,这也应该解决字段分辨率。
编辑:
此外,您应该在type
内定义geometry
,如下所示:
antenneSchema
或
geometry: geoSchema