首页 > 解决方案 > 如何避免 OVER_QUERY_LIMIT nodejs

问题描述

每次当我想在我的数据库中创建新位置时,我都会遇到这个错误,我试图实现 setTimeout() 来避免这种情况,但如果我做得正确,我不会感到沮丧。有什么建议吗?

router.post("/",middleware.isLoggedIn ,upload.single("image"),function(req, res){
 
    geocoder.geocode(req.body.place.location, function(err, data){
        while(status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
        {      
            setTimeout(3000);
        }  
        if ( err){   
            req.flash("error", err.message);
            console.log("error");  
            return res.redirect("back");
        }
        req.body.place.lat = data[0].latitude;
        req.body.place.lng = data[0].longitude;
    //cloudinary configuration    
    cloudinary.uploader.upload(req.file.path, function(result){
        // add cloudinary url for the image to the campground object under image property
        req.body.place.image = result.secure_url;
        // add author to campground
        req.body.place.author = {
            id: req.user._id,
            username: req.user.username
        };

        Place.create(req.body.place, function(err, newlyCreated){
            if(err){
                res.send(err);
            }else{
                res.redirect("/");
            }
        });
    });
  });
});

标签: javascriptnode.jsgoogle-mapsgeolocation

解决方案


来自关于查询限制的 GOOGLE 文档

“收到状态码为 OVER_QUERY_LIMIT 的响应后,您的应用程序应确定已超出哪个使用限制。这可以通过暂停 2 秒并重新发送相同的请求来完成。如果状态码仍为 OVER_QUERY_LIMIT,则您的应用程序发送的请求过多每天。否则,您的应用程序每秒发送的请求太多。”

您的第一步需要是 2 秒超时来确定原因。如果每秒太多,请减少直到不再出现错误。

光盘&


推荐阅读