我正在尝试使用流星redux中间件。循序渐进地学习了这些教程,但到目前为止还没有使它正常工作。问题似乎出在startSubscription上:我再也看不到get()和subscribe()函数的调用,我将控制台调用滑入浏览器和服务器终端中都为空的内部。
// 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;
}
})
);
};
我可能忽略了一些愚蠢的事情。有人可以帮我吗?
答案 0 :(得分:0)
我终于意识到了问题。
首先,我的store.js文件出现问题。具体来说,我无意间删除了与createReactiveMiddlewares相关的行。
const {来源,订阅,} = createReactiveMiddlewares(Tracker);
然后,在实现actions.js时,您必须非常注意动作的命名以及根据动作命名的Meteor.subscribe方法。例如,如果HOME_POSTS_SUB等于“ home_randomName”,则它将不起作用,因为_SUBSCRIPTION_READY和_SUBSCRIPTION_CHANGED前缀必须与_SUB字符串值保持一致。
export const HOME_POSTS_SUBSCRIPTION_READY = 'HOME_POSTS_SUBSCRIPTION_READY';导出常量 HOME_POSTS_SUBSCRIPTION_CHANGED ='HOME_POSTS_SUBSCRIPTION_CHANGED'; export const HOME_POSTS_SUB ='home.posts'; // 将工作! //出口 const HOME_POSTS_SUB ='home.randomName'; //被宠坏了! //导出const HOME_POSTS_SUB ='帖子'; //被宠坏了!
export const loadHomePosts =()=> startSubscription({ 密钥:HOME_POSTS_SUB, 订阅:()=> Meteor.subscribe(HOME_POSTS_SUB), get:()=> Posts.find()。fetch(),
给HOME_POSTS_命名...会给您带来很多麻烦 后来。因此,正确命名动作会在以后 在reducer.js文件中使用它们:
导出函数home(state = initialState,action){开关 (action.type){ 案例HOME_POSTS_SUBSCRIPTION_READY: 返回{ ...州, 准备好了:action.payload.ready, postsSubscriptionStopped:否, }; 案例HOME_POSTS_SUBSCRIPTION_CHANGED: 返回{ ...州, 帖子:action.payload, }; 情况STOP_SUBSCRIPTION: 返回action.payload === HOME_POSTS_SUB ? {...状态,postsSubscriptionStopped:true} :状态; 默认: 返回状态; }