在我使用react-router
的React / Meteor应用程序中,我试图将URL参数(例如URL:/user/P8HDQMAuapRESoWo6
)传递给createContainer
,它获取第一个MongoDB的结果查询
let user = Meteor.users.findOne({ _id: this.props.params.id});
运行第二个MongoDB查询
users: Meteor.users.find(
{
'location': {
$near: {
$geometry: {
'type': 'Point',
'coordinates': user.location.coordinates
...
然而,这会导致错误
Uncaught TypeError: Cannot read property 'params' of undefined
this.props.params
?Meteor.users.findOne()
内运行第一个Mongodb查询createContainer
吗?User.jsx
import React, { Component, PropTypes } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
export class User extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
Something
</div>
)
}
}
export default createContainer(() => {
Meteor.subscribe('allUsers')
let user = Meteor.users.findOne({ _id: this.props.params.id});
return {
users: Meteor.users.find(
{
'location': {
$near: {
$geometry: {
'type': 'Point',
'coordinates': user.location.coordinates
},
$maxDistance: 400
}
}
}
).fetch()
}
}, User);
可以像这样包装reuturn
函数吗?
export default createContainer(({params}) => {
Meteor.subscribe('allUsers')
let user = Meteor.users.findOne({ _id: params.id});
if(user) {
return {
users: Meteor.users.find(
{
'location': {
$near: {
$geometry: {
'type': 'Point',
'coordinates': user.location.coordinates
},
$maxDistance: 400
}
}
}
).fetch()
}
} else {
return { users: [] }
}
}, User);
答案 0 :(得分:4)
createContainer
接受来自react-router(或任何父)的props作为函数参数。这应该有效:
export default createContainer(({params}) => {
Meteor.subscribe('allUsers')
let user = Meteor.users.findOne({ _id: params.id});
return {
users: Meteor.users.find(
{
'location': {
$near: {
$geometry: {
'type': 'Point',
'coordinates': user.location.coordinates
},
$maxDistance: 400
}
}
}
).fetch()
}
}, User);