Javascript 如何在redux Reducer中区分来自rails后端api控制器中单个操作的不同实例变量
好的,在我的可能匹配控制器中有一个动作,名为setup\u可能匹配,它呈现jsonJavascript 如何在redux Reducer中区分来自rails后端api控制器中单个操作的不同实例变量,javascript,ruby-on-rails,reactjs,redux,Javascript,Ruby On Rails,Reactjs,Redux,好的,在我的可能匹配控制器中有一个动作,名为setup\u可能匹配,它呈现json def setup_possible_matches @contemplated_piece = @visible_gor_clothing || GorClothing.where(gender: :male).order(created_at: :desc).first @standalone_bottoms = GorClothing.where('standalone = ?', true).
def setup_possible_matches
@contemplated_piece = @visible_gor_clothing || GorClothing.where(gender: :male).order(created_at: :desc).first
@standalone_bottoms = GorClothing.where('standalone = ?', true).where('merch_type = ?', 'bottom').where.not('id = ?', @contemplated_piece).where(gender: :male).order(created_at: :desc)
@standalone_tops = GorClothing.where('standalone = ?', true).where('merch_type = ?', 'top').where.not('id = ?', @contemplated_piece).where(gender: :male).order(created_at: :desc)
@suggested_tops = GorClothing.joins(:toggled_pieces).where('merch_type = ?', 'top').where('possible_matches.contemplated_piece_id = ?', @contemplated_piece.id).where(gender: :male).order(created_at: :desc)
@suggested_bottoms = GorClothing.joins(:toggled_pieces).where('merch_type = ?', 'bottom').where('possible_matches.contemplated_piece_id = ?', @contemplated_piece.id).where(gender: :male).order(created_at: :desc)
@extra_tops = GorClothing.joins(:toggled_pieces).where('merch_type = ?', 'top').where.not('possible_matches.contemplated_piece_id = ?', @contemplated_piece.id).where('standalone = ?', false).where(gender: :male).order(created_at: :desc)
@extra_bottoms = GorClothing.joins(:toggled_pieces).where('merch_type = ?', 'bottom').where.not('possible_matches.contemplated_piece_id = ?', @contemplated_piece.id).where('standalone = ?', false).where(gender: :male).order(created_at: :desc)
render json: {contemplated_piece: @contemplated_piece, standalone_tops: @standalone_tops, standalone_bottoms: @standalone_bottoms, suggested_tops: @suggested_tops, suggested_bottoms: @suggested_bottoms, extra_tops: @extra_tops, extra_bottoms: @extra_bottoms}
end
在db/seeds.rb中,我使用以下内容为GorClothing数据库添加种子:
GorClothing.create(merch_type: 'top', description: 'Perfect for a night out with the boys', gender: 'male', sizes: 'M', colors_available: 'Gray', price: '$59.99', standalone: true, quantity: 5, images_attributes: [{type_of_image: 1, picture: File.open(File.join(Rails.root, 'app/assets/images/CoolNightClubShirt.png'))}])
GorClothing.create(merch_type: 'top', description: 'Bon Vivant', gender: 'male', sizes: 'M', colors_available: 'black', price: '$64.99', standalone: true, quantity: 6, images_attributes: [{type_of_image: 1, picture: File.open(File.join(Rails.root, 'app/assets/images/CoolPartyHostShirt.png'))}])
GorClothing.create(merch_type: 'top', description: 'At the beach bar ordering martinis; Learning how to salsa dance; This piece simply communicates that we are unbothered with the things that might weigh down others', gender: 'male', sizes: 'M', colors_available: 'black', price: '$64.99', standalone: true, quantity: 6, images_attributes: [{type_of_image: 1, picture: File.open(File.join(Rails.root, 'app/assets/images/CoolWhiteShirt.png'))}])
GorClothing.create(merch_type: 'top', description: 'To a baseball game, to a concert, to a get-together, this has casual written all over it.', gender: 'male', sizes: 'M', colors_available: 'black', price: '$64.99', standalone: true, quantity: 6, images_attributes: [{type_of_image: 1, picture: File.open(File.join(Rails.root, 'app/assets/images/DopeCasualShirt.png'))}])
GorClothing.create(merch_type: 'top', description: 'Honored guest much?', gender: 'male', sizes: 'M', colors_available: 'Gray', price: '$49.99', standalone: true, quantity: 4, images_attributes: [{type_of_image: 1, picture: File.open(File.join(Rails.root, 'app/assets/images/CoolWineTastingShirt.png'))}])
GorClothing.create(merch_type: 'top', description: 'Too Fresh. Like Fresh Prince of Bel-Air Fresh. Okay, lemme stop.', gender: 'male', sizes: 'M', colors_available: 'White', price: '$39.99', standalone: true, quantity: 4, images_attributes: [{type_of_image: 1, picture: File.open(File.join(Rails.root, 'app/assets/images/FunkyTropicalShirt.png'))}])
在views/apability_matches/setup_apability_matches.json.builder文件中,我有:
json.contemplated_piece @contemplated_piece :standalone, :merch_type, :gender, :price, :created_at
json.standalone_bottoms @standalone_bottoms :merch_type, :gender, :price, :created_at
json.standalone_tops @standalone_tops :standalone, :merch_type, :gender, :price, :created_at
hash = {:toggled_pieces => {:contemplated_piece_id => @contemplated_piece.id}}
json.suggested_tops @suggested_tops :merch_type, :gender, :created_at, :price json.merge hash
hash - {:toggled_pieces => {:contemplated_piece_id => @contemplated_piece.id}}
json.suggested_bottoms @suggested_bottoms :merch_type, :price, :gender, :created_at, json.merge hash
json.extra_tops @extra_tops :merch_type, :standalone, :gender, :price, :created_at
json.extra_bottoms @extra_bottoms :merch_type, :standalone, :price, :gender, :created_at
该操作由actions/index.js文件中的redux操作调用
export function defaultPieces(){
return function(dispatch){
fetch(`${API_URL}/possible_matches/setup_possible_matches`, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then((res) => res.json())
.then((json) => {
dispatch(getInitialPieces(json))
})
}
}
export function getInitialPieces(request){
return {
type: INITIAL_PIECES,
payload: request
}
}
我试图让mapStateToProps方法理解不同实例变量之间的差异,这些变量是在rails后端api中的possibleMatches控制器内部的任何特定操作中定义的,即上面的setup_mablue_matches
function mapStateToProps(state){
return {
UpperComponents: state.possibleMatches.UpperComponents,
LowerComponents: state.possibleMatches.LowerComponents,
contemplatedPiece: state.possibleMatches.contemplated_piece,
extraTops: state.possibleMatches.extraTops,
extraBottoms: state.possibleMatches.extraBottoms,
standaloneTops: state.possibleMatches.standaloneTops,
standaloneBottoms: state.possibleMatches.standaloneBottoms,
suggestedTops: state.possibleMatches.suggestedTops,
suggestedBottoms: state.possibleMatches.suggestedBottoms
};
}
在reducers/index.js中,我们有:
const allReducers = combineReducers({
possibleMatches: PossibleMatchesReducer,
routing: routerReducer,
form: formReducer
});
在控制台中,我得到一个错误:
未捕获(承诺中)TypeError:无法读取未定义的属性“预期的\u片段”
它是指可能匹配减速器内的一条线:
case INITIAL_PIECES:
console.log('Initial_Pieces: ', action.payload);
return Object.assign({}, state, {contemplated_piece: action.payload.data.contemplated_piece}, <-- this line
{extraTops: action.payload.data.extra_tops},
{extraBottoms: action.payload.data.extra_bottoms},
{standaloneTops: action.payload.data.standalone_tops},
{standaloneBottoms: action.payload.data.standalone_bottoms},
{suggestedTops: action.payload.data.suggested_tops},
{suggestedBottoms: action.payload.data.suggested_bottoms})
任何帮助都将不胜感激。谢谢。所以,我发现rails控制器中有一个愚蠢的before_操作过滤器,它提示首先调用另一个方法,该方法确实呈现json,但在一个散列中有两个数组。这就是为什么我在控制台中得到了结果(我在上面发布的内容)。我只需要从图片中获取这些信息,然后返回正确的数据。请提供调用
可能匹配还原程序的代码。如果您在这个reducerconsole.log(action.payload)
中添加调试信息,将非常有用。我假设并没有数据,而且正如我看到的那个样,默认的pieces函数什么也不返回。当你说在调用可能匹配还原器的地方提供代码时,试着让promiseIm对你所说的有点困惑。在上面。defaultPieces调用getInitialPieces,getInitialPieces返回一个类型:INITIAL_PIECES,因此PossibleMatches reducer中的大小写INITIAL_PIECES与之对应。我将用reducer.Rails中的console.log语句更新上面的问题,Rails不应该给出这样的结构。你能调试console.log(json)
上面的dispatch(getInitialPieces(json))
调用吗?我的意思是应该有json对象作为响应,你有一个数组,根据console.log
class PossibleMatches extends Component{
constructor(props){
super(props);
this.props.defaultPieces().then(function(results){console.log('Results: ', results)});
}
...rest of Component
function mapDispatchToProps(dispatch) {
return {
defaultPieces: () => {
dispatch(defaultPieces())
}
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(PossibleMatches)