【发布时间】:2017-11-23 02:25:39
【问题描述】:
目前我能够对性能进行相当多的优化,但还是有点慢:/
最新编辑:
我目前的解决方案(最快的 atm(但仍然很慢)并保持秩序):
服务器
router.post('/images', function(req, res, next) {
var image = bucket.file(req.body.image);
image.download(function(err, contents) {
if (err) {
console.log(err);
} else {
var resultImage = base64_encode(contents);
var index = req.body.index;
var returnObject = {
image: resultImage,
index: index
}
res.send(returnObject);
}
});
});
客户端查询
$scope.getDataset = function() {
fb.orderByChild('id').startAt(_start).limitToFirst(_n).once("value", function(dataSnapshot) {
dataSnapshot.forEach(function(childDataSnapshot) {
_start = childDataSnapshot.child("id").val() + 1;
var post = childDataSnapshot.val();
var image = post.image;
var imageObject = {
image: image,
index: position
};
position++;
$.ajax({
type: "POST",
url: "images",
data: imageObject,
}).done(function(result) {
post.image = result.image;
$scope.data[result.index] = post;
$scope.$apply();
firstElementsLoaded = true;
});
})
});
};
客户端 HTML
<div ng-controller="ctrl">
<div class="allcontent">
<div id="pageContent" ng-repeat="d in data track by $index"><a href="details/{{d.key}}" target="_blank"><h3 class="text-left">{{d.title}}<a href="../users/{{d.author}}"><span class="authorLegend"><i> by {{d.username}}</i></span></a></h3>
</a>
<div class="postImgIndex" ng-show="{{d.upvotes - d.downvotes > -50}}">
<a href="details/{{d.key}}" target="_blank"><img class="imgIndex" ng-src="data:image/png;base64,{{d.image}}"></a>
</div>
<div class="postScore">{{d.upvotes - d.downvotes}} HP</div>
</div>
</div>
</div>
【问题讨论】:
-
什么是
bucket.file()?我不确定那是什么包裹。 -
@PatrickRoberts 忘记添加了。和上次一样。问题已编辑。
-
查看文档和代码后,这与上次相同。你
push()将每个结果发送给$scope.data,就像你对here和fileArray所做的一样。鉴于此,你能弄清楚如何以类似的方式解决这个问题吗?你应该尝试学习如何识别和解决这个问题,这样你就不必每次都问。 -
@PatrickRoberts 我的主要问题是我不知道如何处理服务器和客户端之间的不同分段
-
你的服务器代码很好,只是你的firebase代码有点乱。我一直在寻找如何至少获取每个快照的索引,但显然我对快照的工作方式有一个基本的误解,但请查看stackoverflow.com/questions/26471666/… 了解如何获取对每个孩子的引用。
标签: javascript node.js firebase google-cloud-platform google-cloud-storage