Node.js 我的两个变量有时不相等,但必须相等

Node.js 我的两个变量有时不相等,但必须相等,node.js,asynchronous,Node.js,Asynchronous,我的两个变量“soundTypeFound[0].name”和“req.soundTypes[iteration]”每次都必须相同,但每五次就有一次为false 我认为循环比函数“allSoundTypeQuery.GetSoundTypeByName(item)(soundtypefind,err)=>{}”运行得更快,变量“iteration”的增量时间与循环的时间不一样 谢谢你的帮助 异步函数检查声音类型(req、res、声音类型字符串、错误、下一步){ 让停止=错误; req.sound

我的两个变量“soundTypeFound[0].name”和“req.soundTypes[iteration]”每次都必须相同,但每五次就有一次为false

我认为循环比函数“allSoundTypeQuery.GetSoundTypeByName(item)(soundtypefind,err)=>{}”运行得更快,变量“iteration”的增量时间与循环的时间不一样

谢谢你的帮助

异步函数检查声音类型(req、res、声音类型字符串、错误、下一步){
让停止=错误;
req.soundTypes=soundtypessString.split(',');
设迭代=0;
用于(请求声音类型的let项){
等待allSoundTypeQuery.GetSoundTypeByName(项,(soundTypeFound,err)=>{
如果(错误){
返回res.status(500.json)({“error”:“在DB中找不到声音类型”});
}

如果(soundtypefind.length因为您发送了一个回调函数,它不会等待并继续下一次迭代,因此会导致错误,现在该方法将返回promisify,因为我们正在使用util模块的promisify方法,而不是等待promise解析,然后移动到下一行

const {promisify} = require('util');

async function checkSoundTypes (req, res, soundTypesString, error, next) {
let stop = false;
req.soundTypes = soundTypesString.split(',');

let iteration = 0;
for (let item of req.soundTypes) {
    const getSoundTypeByName = promisify(allSoundTypeQuery.GetSoundTypeByName);
    const soundTypeFound= await getSoundTypeByName(item);
    if (!soundTypeFound) { // this you should check as per your response
        return res.status(500).json({"error": "Cannot find the sound type in the DB"});
    }

    if (soundTypeFound.length <= 0 || soundTypeFound[0].name !== req.soundTypes[iteration]) {
        stop = true;
    }
    iteration++;
    if (iteration === req.soundTypes.length) {
        if (stop === true) {
            error.push("soundTypes");
            return res.status(400).json(error);
        }else if (err) {
            return res.status(400).json(error);
        }else {
            next();
        }
    }
}
const{promisify}=require('util');
异步函数检查声音类型(req、res、声音类型字符串、错误、下一步){
让停止=错误;
req.soundTypes=soundtypessString.split(',');
设迭代=0;
用于(请求声音类型的let项){
const getSoundTypeByName=promisify(allSoundTypeQuery.getSoundTypeByName);
const soundTypeFound=等待getSoundTypeByName(项);
如果(!soundTypeFound){//,则应根据您的回答进行检查
返回res.status(500.json)({“error”:“在DB中找不到声音类型”});
}

如果(soundtypefind.length因为您发送了一个回调函数,它不会等待并继续下一次迭代,因此会导致错误,现在该方法将返回promisify,因为我们正在使用util模块的promisify方法,而不是等待promise解析,然后移动到下一行

const {promisify} = require('util');

async function checkSoundTypes (req, res, soundTypesString, error, next) {
let stop = false;
req.soundTypes = soundTypesString.split(',');

let iteration = 0;
for (let item of req.soundTypes) {
    const getSoundTypeByName = promisify(allSoundTypeQuery.GetSoundTypeByName);
    const soundTypeFound= await getSoundTypeByName(item);
    if (!soundTypeFound) { // this you should check as per your response
        return res.status(500).json({"error": "Cannot find the sound type in the DB"});
    }

    if (soundTypeFound.length <= 0 || soundTypeFound[0].name !== req.soundTypes[iteration]) {
        stop = true;
    }
    iteration++;
    if (iteration === req.soundTypes.length) {
        if (stop === true) {
            error.push("soundTypes");
            return res.status(400).json(error);
        }else if (err) {
            return res.status(400).json(error);
        }else {
            next();
        }
    }
}
const{promisify}=require('util');
异步函数检查声音类型(req、res、声音类型字符串、错误、下一步){
让停止=错误;
req.soundTypes=soundtypessString.split(',');
设迭代=0;
用于(请求声音类型的let项){
const getSoundTypeByName=promisify(allSoundTypeQuery.getSoundTypeByName);
const soundTypeFound=等待getSoundTypeByName(项);
如果(!soundTypeFound){//,则应根据您的回答进行检查
返回res.status(500.json)({“error”:“在DB中找不到声音类型”});
}
如果(soundTypeFound.length