访问Meteor-React的createContainer

时间:2016-12-31 01:15:25

标签: javascript mongodb reactjs meteor meteor-react

在我使用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
  1. 我们如何访问网址参数或this.props.params
  2. 我们应该在Meteor.users.findOne()内运行第一个Mongodb查询createContainer吗?
  3. 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);
    

1 个答案:

答案 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);