Node.js 使用async/await时,Mongoose insertMany从不返回
我正面临一个奇怪的问题,在谷歌搜索了几个小时后,我提交了这个问题,希望a)我能马上找到答案,或者b)聪明的头脑会出现并拯救我 我正在通过Express中的Bull任务查询Steam API。在我将代码改为使用async/await之前,一切都很顺利,现在Mongoose的Node.js 使用async/await时,Mongoose insertMany从不返回,node.js,mongodb,express,mongoose,async-await,Node.js,Mongodb,Express,Mongoose,Async Await,我正面临一个奇怪的问题,在谷歌搜索了几个小时后,我提交了这个问题,希望a)我能马上找到答案,或者b)聪明的头脑会出现并拯救我 我正在通过Express中的Bull任务查询Steam API。在我将代码改为使用async/await之前,一切都很顺利,现在Mongoose的insertMany没有返回任何内容 以下是Bull任务处理器文件: const SteamApp = require('./../models/steamapp.model'); const SteamAPI = requir
insertMany
没有返回任何内容
以下是Bull任务处理器文件:
const SteamApp = require('./../models/steamapp.model');
const SteamAPI = require('./../lib/steam.lib');
module.exports = async function(job) {
console.log("--------------------------- STARTING THE JOB!");
await job.progress(5);
try {
const steamData = await new SteamAPI().getAppList();
await job.progress(10);
if(steamData && steamData.applist && steamData.applist.apps && steamData.applist.apps.length) {
const appsPerProgressPoint = Math.ceil(steamData.applist.apps.length / 90);
let appList = steamData.applist.apps;
let numberOfAppsInserted = 0;
let taskProgress = 10;
while(appList.length) {
const appsToInsert = appList.splice(0, appsPerProgressPoint);
const documentsInserted = await SteamApp.insertMany(appsToInsert, { ordered: false });
if(documentsInserted && documentsInserted.length) {
console.log(`Inserted ${documentsInserted.length} new Steam apps`);
numberOfAppsInserted += documentsInserted.length;
} else {
console.log("Inserted no apps this time.");
}
taskProgress++;
await job.progress(taskProgress);
}
return `${numberOfAppsInserted} new apps were inserted`;
} else {
console.log("Received the following data from Steam: ", steamData);
return 'The Steam app list was empty or there was an error.';
}
} catch(err) {
return Promise.reject(new Error(`The error ${err} occurred and the job failed.`));
}
}
SteamApp型号:
const mongoose = require('mongoose');
const steamAppSchema = new mongoose.Schema( {
appid: { type: Number, unique: true },
name: String,
type: String,
isFree: Boolean
} );
module.exports = mongoose.model('SteamApp', steamAppSchema);
SteamAPI帮助程序:
const axios=require('axios');
require('dotenv').config()
我已经尝试将此作为一个最小的代码示例,以便可能缺少错误控制、验证等。代码在constdocumentsinserted=wait-SteamApp.insertMany(appsToInsert,{ordered:false})处停止代码>行和insertMany
函数从不插入或返回任何内容。请注意,如果改用insertMany回调函数,函数将正常工作,数据将被插入,我将得到回调函数中插入的内容
axios.get
函数也使用wait并正常工作。所以我的直觉告诉我这和猫鼬有关,但我不知道到底是什么
使用以下命令:
- 节点12.18.4
- 猫鼬5.10.7
- MongoDB 3.6.3
- 快报4.16.1
- 公牛3.18.0
- Axios 0.20.0
编辑:appsToInsert
是一个对象数组,每个对象包含两个属性:appid
,一个数字;name
,一个字符串。简言之
appsToInsert = [{
appid: 3939,
name: 'A random name'
}, {
appid: 298489,
name: 'Yet another game name'
}, ...];
更新1:为了更好地理解这个问题,我尝试了很多不同的方法,将Bull外部处理器中的代码直接移动到this.queue.process
inline函数中。现在一切都好了。因此,我猜问题与Bull如何在子进程沙箱中管理异步/等待操作有关。我将继续摆弄它,看看是否可以使用外部处理器提出解决方案。appsToInsert的结构是什么?
?是的,我忘了提到,谢谢提醒我。我将编辑问题以添加有关appsToInsert
的详细信息。您确定appToInsert
与架构完全匹配吗?另外,您说过,当您使用insertMany
回调时,它会工作。你能给我们看一下代码吗?那会使文章的长度增加一倍。我将尝试设置gist或公共github存储库。好的。请这样我们就可以知道可能出了什么问题。
appsToInsert = [{
appid: 3939,
name: 'A random name'
}, {
appid: 298489,
name: 'Yet another game name'
}, ...];