【问题标题】:I can't stop the asynchronous我无法停止异步
【发布时间】:2016-09-26 16:11:57
【问题描述】:

所以我试图获得一个带有循环的数组,而 nodejs 的异步特性让我很生气。这是我的代码:

	getDevices(userIDs, function(result) {
		 if (result) {
		 	sendNotification(messageUser, messageText, result);
		 	res.send("Success");
		 } else {
		 	res.send("ERROR");
		 }
	});
	
});

function getDevices(userIDs, callback) {
	var userDevices = [];
	var device = [];
	
	for (var i = 0; i < userIDs.length; i++) {
			searchRegisterDevices(userIDs[i], function(result) {
				if (result) {
					for (var j = 0; j < result.length; j++) {
						device = {platform: result[j].platform, token: result[j].token};
						userDevices.push(device);		
					}
				} else {
					console.log("ERROR");
				}
			});	
	}
	callback(userDevices);
}

function searchRegisterDevices(userID, callback) {
	MongoClient.connect(url, function(err, db) {
		if (err) {
			console.log(err);
		} else {
			console.log("We are connected");
		}
		
		var collection = db.collection('RegisteredDevices');
		collection.find({userID: userID}).toArray(function (err, result) {
			if (err) {
            	console.log("Error: " + err);
        	} else if (result.length) {
				callback(result);
       	 	} else {
            	console.log('No document found');
        	}
        	db.close();
    	});
	});

我首先需要从我的 mongodb 集合中取出与 userID 中的 ID 匹配的所有设备。 SO userIDs 是一组与集合中的设备相关联的 ID。获得设备后,我需要从返回的对象中获取设备令牌。

所以: 1) 调用 getDevices 传递用户 ID 数组 2) 使用设备 ID 调用 searchRegisterDevices。 3) searchRegisterDevices 返回一个设备数组。 4) 从该数组中获取设备令牌并推送到 userDevices 数组。 5) 返回用户设备数组 6) 使用 userDevices 数组调用 sendNotification

我知道我的问题,我只是很难解决它们

【问题讨论】:

    标签: javascript android arrays node.js mongodb


    【解决方案1】:

    您应该使用单个查询来获取它们,而不是为每个用户获取用户设备: 第一:它将减少通话次数 第二:它将节省您处理回调o / p。

    为此使用 $in 运算符。

    更改 searchdevices 方法:

    function searchRegisterDevices(userID, callback) {
        MongoClient.connect(url, function(err, db) {
                if (err) {
                    console.log(err);
                } else {
                    console.log("We are connected");
                }
    
                var collection = db.collection('RegisteredDevices');
                collection.find({
                        userID: {
                            $in: userIDs
                        }).toArray(function(err, result) {
                        if (err) {
                            console.log("Error: " + err);
                        } else if (result.length) {
                            callback(result);
                        } else {
                            console.log('No document found');
                        }
                        db.close();
                    });
                });
        }
    

    它将为传递的用户 ID 返回用户设备数组。

    【讨论】:

    • 起初我会抱怨说这行不通。然后它击中了我。这很完美。我不敢相信我没有想到这一点。这不是搜索每个设备,而是返回每个 ID 的每个设备的数组。完美的。点赞!
    • @AustinHunter 很高兴它对您有所帮助。 :)
    猜你喜欢
    • 2021-12-27
    • 2013-03-14
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多