[英]Accessing URL Parameters in Meteor-React's createContainer
In my React/Meteor app that uses react-router
, I am trying to pass the URL parameters (example URL: /user/P8HDQMAuapRESoWo6
) into createContainer
, which fetches the result of the first MongoDB query 在我的使用
react-router
React / Meteor应用程序中,我试图将URL参数(例如URL: /user/P8HDQMAuapRESoWo6
) /user/P8HDQMAuapRESoWo6
给createContainer
,后者将获取第一个MongoDB查询的结果
let user = Meteor.users.findOne({ _id: this.props.params.id});
to run the second MongoDB query 运行第二个MongoDB查询
users: Meteor.users.find(
{
'location': {
$near: {
$geometry: {
'type': 'Point',
'coordinates': user.location.coordinates
...
However this results in the error 但是,这会导致错误
Uncaught TypeError: Cannot read property 'params' of undefined
this.props.params
? this.props.params
? Meteor.users.findOne()
inside createContainer
? createContainer
运行第一个Mongodb查询Meteor.users.findOne()
? User.jsx 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);
Is it ok to wrap the reuturn
function like this? 这样包装
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
accepts the props from react-router (or any parent) as function arguments. createContainer
接受来自react-router(或任何父对象)的props作为函数参数。 This should work: 这应该工作:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.