我一直在我的应用程序的后端工作。在这一点上,它可以访问一个数据库中的所有数据,并输出它。我试图实现一些查询,以便用户可以过滤出返回的内容。我的DAL/DAO,看起来像这样
let mflix //Creates a variable used to store a ref to our DB
class MflixDAO {
static async injectDB(conn){
if(mflix){
return
}
try{
mflix = await conn.db(process.env.JD_NS).collection("movies")
}catch(e){
console.error('Unable to establish a collection handle in mflixDAO: ' + e)
}
}
// Creates a query to fetch data from the collection/table in the DB
static async getMovies({
mflix.controller
filters = null,
page = 0,
moviesPerPage = 20,
} = {}) {
let query
if (filters){
// Code
if("year" in filters){
query = {"year": {$eq: filters["year"]}}
}
// Code
}
// Cursor represents the returned data
let cursor
try{
cursor = await mflix.find(query)
}catch(e){
console.error('Unable to issue find command ' + e)
return {moviesList: [], totalNumMovies: 0}
}
const displayCursor = cursor.limit(moviesPerPage).skip(moviesPerPage * page)
try{
const moviesList = await displayCursor.toArray() // Puts data in an array
const totalNumMovies = await mflix.countDocuments(query) // Gets total number of documents
return { moviesList, totalNumMovies}
} catch(e){
console.error('Unable to convert cursor to array or problem counting documents ' + e)
return{moviesList: [], totalNumMovies: 0}
}
}
}
export default MflixDAO
正如您所知道的,我使用的是MongoDBAtlas中的一个示例数据库,我使用Postman测试HTTP请求。
无论如何,当我执行一个基本的GET请求时。程序运行时没有任何问题。所有的数据都按预期输出。但是,如果我执行一些沿着于
GET http://localhost:5000/api/v1/mflix?year=1903
然后moviesList
返回一个空数组[]
,但没有错误消息。
调试后,我怀疑问题出在cursor = await mflix.find(query)
或displayCursor = cursor.limit(moviesPerPage).skip(moviesPerPage * page)
上,但这些方法的调用堆栈对我来说太复杂了,我甚至不知道该查找什么。
有什么建议吗?
编辑:下面是我尝试访问的文档的示例:
{
"_id": "573a1390f29313caabcd42e8",
"plot": "A group of bandits stage a brazen train hold-up, only to find a determined posse hot on their heels.",
"genres": [
"Short",
"Western"
],
"runtime": 11,
"cast": [
"A.C. Abadie",
"Gilbert M. 'Broncho Billy' Anderson",
"George Barnes",
"Justus D. Barnes"
],
"poster": "https://m.media-amazon.com/images/M/MV5BMTU3NjE5NzYtYTYyNS00MDVmLWIwYjgtMmYwYWIxZDYyNzU2XkEyXkFqcGdeQXVyNzQzNzQxNzI@._V1_SY1000_SX677_AL_.jpg",
"title": "The Great Train Robbery",
"fullplot": "Among the earliest existing films in American cinema - notable as the first film that presented a narrative story to tell - it depicts a group of cowboy outlaws who hold up a train and rob the passengers. They are then pursued by a Sheriff's posse. Several scenes have color included - all hand tinted.",
"languages": [
"English"
],
"released": "1903-12-01T00:00:00.000Z",
"directors": [
"Edwin S. Porter"
],
"rated": "TV-G",
"awards": {
"wins": 1,
"nominations": 0,
"text": "1 win."
},
"lastupdated": "2015-08-13 00:27:59.177000000",
"year": 1903,
"imdb": {
"rating": 7.4,
"votes": 9847,
"id": 439
},
"countries": [
"USA"
],
"type": "movie",
"tomatoes": {
"viewer": {
"rating": 3.7,
"numReviews": 2559,
"meter": 75
},
"fresh": 6,
"critic": {
"rating": 7.6,
"numReviews": 6,
"meter": 100
},
"rotten": 0,
"lastUpdated": "2015-08-08T19:16:10.000Z"
},
"num_mflix_comments": 0
}
编辑:这似乎是一个数据类型问题。当我请求一个字符串/varchar类型的数据时,程序返回包含该值的值。例如:
Input:
GET localhost:5000/api/v1/mflix?rated=TV-G
Output:
{
"_id": "XXXXXXXXXX"
// Data
"rated" = "TV-G"
// Data
}
编辑:这个问题似乎与我之前发布的任何内容都没有关系。问题出在这段代码中:
let filters = {}
if(req.query.year){
filters.year = req.query.year // This line needs to be changed
}
const {moviesList, totalNumMovies} = await MflixDAO.getMovies({
filters,
page,
moviesPerPage,
})
我会在下面的答案中解释
1条答案
按热度按时间km0tfn4u1#
问题是,当我发出一个HTTP请求时,请求的值是以字符串的形式传递的。
year
的值被程序注册为字符串。换句话说,DAO最终查找的是"1903"
而不是1903
。当然,year = "1903"
并不存在。要解决这个问题,必须将filters.year = req.query.year
行更改为filters.year = parseInt(req.query.year)
。