gpt4 book ai didi

node.js - 如何延迟铁路由器路由查找

转载 作者:搜寻专家 更新时间:2023-11-01 00:07:03 24 4
gpt4 key购买 nike

考虑以下场景:

有一个 collection称为 Resources特殊用户可以在 admin 上对其进行 CRUD网站。

当普通用户访问网站时,路由会根据对 Resources 的订阅在浏览器中动态创建。 .

我们想暂停路由器查找,直到订阅的回调函数终止。否则,在访问 /<dynamically-created-route> 时会向用户显示“404”直接。

有没有简单的方法来做到这一点?

最佳答案

是这样的吗?您根据资源 ID 创建动态路由,等待具有 id 值的订阅,然后根据您是否可以访问具有该 id 的资源进行路由。这假设您在服务器代码中也有正确的发布设置。

Resources = new Mongo.Collection("resources");

Router.route("/resources/:_id", {
name: "resources",
waitOn: function() {
// Waits on the subscription
return Meteor.subscribe("resources", this.params._id);
},
action: function() {
// Should only return the resource you subscribed to unless you
// subscribe to resources in another part of your application
var resource = Resources.findOne({});
if (resource) {
// The resource exists and the user is able to access it.
this.render("resourceTemplate", {
data: {
resource: resource
}
});
}
else {
Router.go("/404");
}
}
});

关于node.js - 如何延迟铁路由器路由查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com