课程接近完结了,在大部分功能都正常之外,语音查电影或者文字查电影,只要数据库里面有的,是可以查到。如果是数据库没有的,并没有进入保存电影这个步骤,我贴下代码:
exports.searchByDouban = async (q) => {
var options = {
url: 'https://api.douban.com/v2/movie/search?q='
}
options.url += encodeURIComponent(q)
console.log(q)
var response = await request(options)
var data = JSON.parse(response.body)
var subjects = []
var movies = []
if (data && data.subjects) {
subjects = data.subjects
}
if (subjects.length > 0) {
var queryArray = []
subjects.forEach(item => {
console.log(item)
queryArray.push(async () => {
var movie = await Movie.findOne({doubanId: item.id})
if (movie) {
movies.push(movie)
}
else {
var directors = item.directors || []
var director = directors[0] || {}
movie = new Movie({
director: director.name || '',
title: item.title,
doubanId: item.id,
poster: item.images.large,
year: item.year,
genres: item.genres || []
})
movie = await movie.save()
movies.push(movie)
}
})
})
await queryArray
movies.forEach(movie => {
updateMovies(movie)
})
}
return movies
}
上面的subjects打印出来是有值的,但是queryArray.push(async () => {})这个方法体压根没执行啊,里面不管打印什么都没有,导致movies是个空数组,在movie模型里面也添加了相关的字段了。确实不知道问题出在哪里?求老师尽快解答下,让项目早日收尾。