在 Meteor-React 的 createContainer 中访问 URL 参数
Accessing URL Parameters in Meteor-React's createContainer
在我使用 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
- 我们如何访问 URL 参数或
this.props.params
?
- 我们是否应该 运行 第一个 Mongodb 查询
Meteor.users.findOne()
inside 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);
createContainer
接受来自 react-router(或任何父级)的道具作为函数参数。这应该有效:
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);
在我使用 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
- 我们如何访问 URL 参数或
this.props.params
? - 我们是否应该 运行 第一个 Mongodb 查询
Meteor.users.findOne()
insidecreateContainer
?
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);
createContainer
接受来自 react-router(或任何父级)的道具作为函数参数。这应该有效:
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);