我刷新时,Meteor Iron Router会加载两次页面

时间:2016-08-31 03:57:21

标签: javascript html mongodb meteor iron-router

我正在使用流星,mongodb和铁路由器。

当我从main.html导航到profile.html时没有错误,但是当我在profile.html上刷新它加载两次时,开发工具控制台会显示错误,说var hola is not defined,但页面仍然的工作原理。

有什么方法可以解决这个问题吗?

这是我的代码:

main.js / main.html



Router.route('/', {
    name: 'home',
    template: 'home'
});

Template.list_contactList.helpers({
    listName: function () {
        return "";
    },
    list: function () {
        return Contacts.find({});
    }
});

Template.list_contactList.onCreated(function () {
    this.subscribe("list_contactList", {});
});

if (Meteor.isClient) {
    Template.list_contactList.events({
        "click button[name=goDetail]": function (evt, tmpl) {
            console.log(this._id);
            Router.route('profile', {who: this._id});
        }
    });
    
    Template.searchForm.events({
        'submit form': function (event, tmpl) {
         event.preventDefault();
                var textValue = event.target.search.value;
                console.log(textValue);
                event.target.search.value = "";
                HTTP.call('GET', 'https://api.fullcontact.com/v2/person.json?email=' + textValue + '&apiKey=afd6326efc55d447', {},
                    function (error, response) {
                        if (error || response.data.status == 404) {
                        console.log(error);
                        } else {
                            console.log(response.data.contactInfo.fullName);
                            var checkExist = 0;
                            if(response.data.contactInfo.chats != undefined)
                            {
                                if(response.data.contactInfo.chats.length > 0){
                                    for (var i = 0; i < response.data.contactInfo.chats.length; i++) {
                                        if (Contacts.find({name: response.data.contactInfo.fullName, email: response.data.contactInfo.chats[i].handle}).count()) {
                                            checkExist = 1;
                                            break;
                                        }
                                    console.log(checkExist);
                                    }
                                }
                            }
                            var name="", email="";
                            if (checkExist == 0) {
                                name = response.data.contactInfo.fullName;
                                if(response.data.contactInfo.chats != undefined)
                                {
                                    for (var i = 0; i < response.data.contactInfo.chats.length; i++) {
                                        if (response.data.contactInfo.chats[i].handle.includes("@")) {
                                            email = response.data.contactInfo.chats[i].handle;
                                            break;
                                        }
                                    }
                                }
                                Contacts.insert({
                                    name,
                                    email
                                });
                              
                                var getNum = Contacts.find({},{"name":name, "email":email}).fetch();
                                console.log(getNum[0]._id);
                                var id = getNum[0]._id;
                                var photo = "";
                                if(response.data.photos.length > 0)
                                {
                                    for(var i = 0; i < response.data.photos.length; i++) {
                                        photo = response.data.photos[i].url;
                                        Pictures.insert({
                                            id,
                                            photo
                                        });
                                    }
                                }
                                var social = "";
                                if(response.data.socialProfiles.length > 0)
                                {
                                    for(var i = 0; i < response.data.socialProfiles.length; i++) {
                                                                           social = response.data.socialProfiles[i].url;
                                        SocialMedia.insert({
                                            id,
                                            social
                                        });
                                    }
                                }
                            }
                        }
                    });
        }
    });
}
&#13;
<template name="home">
    <div class="col-md-offset-4 col-md-4">
        <h3>Contact</h3> <br />
        {{>searchForm}}
        <br /><br /><br />
        {{>list_contactList}}
    </div>
</template>

<template name="searchForm">
    <form>
        <div class="col-md-9">
            <input class="form-control" type="text" placeholder="Type in email address" name="search">
        </div>
        <input class="btn btn-primary col-md-3" type="submit" value="Submit">
    </form> 
</template>

<template name="list_contactList">
    <br />
    {{listName}} 
    <table class="table table-condensed align-center">
        {{#each list}} 
        <tbody>
            <tr>
                <td>{{_id}}</td>
                <td>{{name}}</td>
                <!--<td>{{email}}</td>-->
                <td><a class="btn btn-large btn-default" href="/profile/{{_id}}">Detail</a></td>
                <!--<td><button class="btn btn-default" name="goDetail">Detail</button></td>-->
            </tr>
        </tbody>
        {{/each}} 
    </table>
</template>
&#13;
&#13;
&#13;

profile.js / profile.html

&#13;
&#13;
var hola;
Router.onBeforeAction('loading');
Router.route('/profile/:_id', {
    template: 'profile',
    data: function () {
        console.log("this is a profile page");
        console.log(this.params._id);
        hola = String(this.params._id);
    }
});

Template.profile_contactList.helpers({
    name: function () {
        console.log(Contacts.findOne({_id: hola}).name);
        return (Contacts.findOne({_id: hola})).name;
    },
    email: function () {
        return Contacts.findOne({_id: hola}).email;
    }
});

Template.profile_pictureList.helpers({
    list: function () {
        return Pictures.find({id:hola});
    }
});

Template.profile_contactList.onCreated(function () {
    this.subscribe("profile_contactList", {});
});

Template.profile_pictureList.onCreated(function () {
    this.subscribe("profile_pictureList", {});
});
&#13;
<template name="profile">
    <div class="col-md-offset-4 col-md-4">
        <h3>Profile</h3>
        <br />
       {{>profile_contactList}}
    </div>
</template>
<template name="profile_contactList">
     <h4>Name: {{name}} <br />
         Email: {{email}}</h4>
    
</template>

<template name="profile_pictureList">
    {{#each list}}
    <img src="{{photo}}" alt="Pictures" style="width:200px;height:200px;">
    {{/each}}
</template>

<!--<template name="profile_socialList">
    {{#each list}}
    {{social}}
    {{/each}}
</template>-->
&#13;
&#13;
&#13;

0 个答案:

没有答案