startSubscription 从未调用过
startSubscription never called in action
我正在尝试使用 meteor 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;
}
})
);
};
我可能忽略了一些愚蠢的事情。有人可以帮我吗?
终于意识到问题了
首先,我的 store.js 文件有问题。具体来说,我不小心删除了与 createReactiveMiddlewares 相关的行。
const { sources, subscriptions, } =
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'; export const
HOME_POSTS_SUBSCRIPTION_CHANGED = 'HOME_POSTS_SUBSCRIPTION_CHANGED';
export const HOME_POSTS_SUB = 'home.posts'; // will work! //export
const HOME_POSTS_SUB = 'home.randomName'; // spoiled!! //export const
HOME_POSTS_SUB = 'posts'; // spoiled!!
export const loadHomePosts = () => startSubscription({
key: HOME_POSTS_SUB,
subscribe: () => Meteor.subscribe(HOME_POSTS_SUB),
get: () => Posts.find().fetch(),
naming HOME_POSTS_... differently will give you a hell lot of troubles
later. So, naming the actions correctly has an effect later when you
use them in the reducer.js file:
export function home(state = initialState, action) { switch
(action.type) {
case HOME_POSTS_SUBSCRIPTION_READY:
return {
...state,
ready: action.payload.ready,
postsSubscriptionStopped: false,
};
case HOME_POSTS_SUBSCRIPTION_CHANGED:
return {
...state,
posts: action.payload,
};
case STOP_SUBSCRIPTION:
return action.payload === HOME_POSTS_SUB
? { ...state, postsSubscriptionStopped: true }
: state;
default:
return state; } }
我正在尝试使用 meteor 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;
}
})
);
};
我可能忽略了一些愚蠢的事情。有人可以帮我吗?
终于意识到问题了
首先,我的 store.js 文件有问题。具体来说,我不小心删除了与 createReactiveMiddlewares 相关的行。
const { sources, subscriptions, } = 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'; export const HOME_POSTS_SUBSCRIPTION_CHANGED = 'HOME_POSTS_SUBSCRIPTION_CHANGED'; export const HOME_POSTS_SUB = 'home.posts'; // will work! //export const HOME_POSTS_SUB = 'home.randomName'; // spoiled!! //export const HOME_POSTS_SUB = 'posts'; // spoiled!!
export const loadHomePosts = () => startSubscription({ key: HOME_POSTS_SUB, subscribe: () => Meteor.subscribe(HOME_POSTS_SUB), get: () => Posts.find().fetch(),
naming HOME_POSTS_... differently will give you a hell lot of troubles later. So, naming the actions correctly has an effect later when you use them in the reducer.js file:
export function home(state = initialState, action) { switch (action.type) { case HOME_POSTS_SUBSCRIPTION_READY: return { ...state, ready: action.payload.ready, postsSubscriptionStopped: false, }; case HOME_POSTS_SUBSCRIPTION_CHANGED: return { ...state, posts: action.payload, }; case STOP_SUBSCRIPTION: return action.payload === HOME_POSTS_SUB ? { ...state, postsSubscriptionStopped: true } : state; default: return state; } }