我正在尝试查询Mongoose能够从数据库中获取的内容。但是,当我在Apollo解析器中返回相同的代码时,它将输出“ Null”。
查询功能
async searchSites(_, {queryString}) {
var pattern = new RegExp(queryString + '.*', "i");
return await Site.find({
name: pattern
});
}
当我console.log
await Site.find({name: pattern})
时,它会在日志中正确打印结果,如下所示:
[
{ _id: '123', name: 'Test', __v: 0 },
{ _id: '1234', name: 'Test', __v: 0 }
]
我正在运行的查询是:
query {
searchSites (queryString:"test") {
_id
name
city
}
}
可能有助于解决问题的其他功能:
TypeDef
type Site {
_id: String!
name: String!
city: String
state: String
country: String
}
extend type Query {
getSiteById(_id: String!): Site,
searchSites(queryString: String!): Site,
getAllSites: [Site]
}
模型
const siteSchema = new Schema({
_id: String,
name: String,
city: String,
state: String,
country: String
});