我正在尝试使用流星redux中间件。循序渐进地学习了这些教程,但是到目前为止还没有使它正常工作。问题似乎出在startSubscription上:我再也看不到调用的get()和subscription()函数,我将控制台调用滑入浏览器和服务器终端中都为空的内部。
// actions.js
import { Meteor } from 'meteor/meteor';
import { startSubscription } from 'meteor-redux-middlewares';
import FaucetRewards from '/imports/api/Rewards/Rewards.js';
export const HOME_REWARDS_SUBSCRIPTION_READY = 'HOME_REWARDS_SUBSCRIPTION_READY';
export const HOME_REWARDS_SUBSCRIPTION_CHANGED = 'HOME_REWARDS_SUBSCRIPTION_CHANGED';
export const HOME_REWARDS_SUB = 'rewards';
export const loadRewards = () =>{
console.log("actions.loadRewards()"); // I can see this
let sub = Meteor.subscribe(HOME_REWARDS_SUB); // I can see this in server console and in the sub variable below
console.log(sub);
console.log(FaucetRewards.find().fetch()); // Sub is not performed in time, so this is empty as expected
return(
startSubscription({
key: HOME_REWARDS_SUB,
get: () => {
console.log("loadRewards.startSubscription.get()"); // I can never see this
return (FaucetRewards.find().fetch())
},
subscribe: () => {
console.log("loadRewards.startSubscription.subcribe()"); // I can never see this
let subscription = Meteor.subscribe(HOME_REWARDS_SUB);
return subscription;
}
})
);
};
我可能忽略了一些愚蠢的事情。有人能帮助我吗?
我终于意识到了问题。
首先,我的store.js文件有问题。具体来说,我无意间删除了与createReactiveMiddlewares相关的行。
const {来源,订阅,} = createReactiveMiddlewares(Tracker);
然后,在实现actions.js时,您必须非常注意动作的命名以及Meteor.subscribe方法的命名,该方法必须根据动作进行命名。例如,如果HOME_POSTS_SUB等于“ home_randomName”,则它将不起作用,因为_SUBSCRIPTION_READY和_SUBSCRIPTION_CHANGED前缀必须与_SUB字符串值保持一致。
导出常量HOME_POSTS_SUBSCRIPTION_READY ='HOME_POSTS_SUBSCRIPTION_READY'; 导出常量HOME_POSTS_SUBSCRIPTION_CHANGED ='HOME_POSTS_SUBSCRIPTION_CHANGED'; export const HOME_POSTS_SUB ='home.posts'; // 将工作!// export const HOME_POSTS_SUB ='home.randomName'; //被宠坏了!// export const HOME_POSTS_SUB ='帖子'; //被宠坏了!
export const loadHomePosts =()=> startSubscription({键:HOME_POSTS_SUB,订阅:()=> Meteor.subscribe(HOME_POSTS_SUB),得到:()=> Posts.find()。fetch(),
命名HOME_POSTS _...的方式不同,以后会给您带来很多麻烦。因此,稍后在reducer.js文件中使用动作时,正确命名动作会产生影响:
导出函数home(state = initialState,action){开关(action.type){case HOME_POSTS_SUBSCRIPTION_READY:返回{... state,ready:action.payload.ready,postsSubscriptionStopped:false,}; 案例HOME_POSTS_SUBSCRIPTION_CHANGED:返回{...状态,帖子:action.payload,}; 情况STOP_SUBSCRIPTION:返回action.payload === HOME_POSTS_SUB吗?{... state,postsSubscriptionStopped:true}:状态;默认值:返回状态;}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句