2015-12-04 178 views
1

我正在尝试构建一个小型流星应用程序。我有一种感觉,它不完全理解服务器 - 客户端关系如何工作的结果。我试图让它工作几个小时,并将其视为设置它的唯一合理方法。这可能有助于补充说我是初学者。流星服务器端HTTP请求

值得一提的是,我在客户端发出http请求时运行正常。

什么是应该发生:

表单提交,表单中的文本发送到通过HTTP请求的API,JSON返回,解析,并返回一个值返回给用户(他们给一个国家代码,它返回一个边界)。除此之外,我想将每个请求存储在带有时间戳的集合中。

任何帮助将不胜感激。

这里是JS:

borders = new Mongo.Collection("borders"); 

if (Meteor.isClient) { 
    // This code only runs on the client 

    //set the session blank before submit action on form. 

    Template.hello.helpers({ 
    borders: function() { 
     // return borders.find({}); 
     // return borders.find({}, {limit: 1}); 
     return Session.get("border"); 
    } 
    }); 

    Template.body.events({ 
    "submit .new-task": function (event) { 
     // Prevent default browser form submit 
     event.preventDefault(); 

     // Get value from form element 
     var countryCode = event.target.text.value; 

     //set form element variable in the session so it can be accessed on the server 
     session.set(countryCode) 

     //invoke the server method 
     Meteor.call("getID", function(error, results) { 
     console.log(results.content); //results.data should be a JSON object 
     }); 

    } 
    }); 
} 

//server-side code 
if (Meteor.isServer) { 
     session.get(countryCode) 
     var url = "http://restcountries.eu/rest/v1/alpha/"+countryCode; 
    Meteor.methods({ 
     getID: function() { 
      this.unblock(); 
      HTTP.get(url, {timeout:30000}, function(error, response) { 
      var respJson = JSON.parse(response.content); 
      console.log(respJson) 
      Session.set("border",respJson["subregion"]) 
      // Insert a task into the collection 
      borders.insert({ 
       text: respJson["borders"], 
       createdAt: new Date() // current time 
      }); 
      // Clear form 
      event.target.text.value = ""; 
      }); 
     } 
    }); 
} 

错误,我得到当应用程序运行:

=> Started proxy.        
=> Started MongoDB.       
W20151203-17:09:54.345(-8)? (STDERR)   
W20151203-17:09:54.346(-8)? (STDERR) /Users/me/.meteor/packages/meteor-tool/.1.1.10.1evms9b++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245 
W20151203-17:09:54.347(-8)? (STDERR)      throw(ex); 
W20151203-17:09:54.347(-8)? (STDERR)       ^
W20151203-17:09:54.347(-8)? (STDERR) ReferenceError: session is not defined 
W20151203-17:09:54.347(-8)? (STDERR)  at borderApp.js:38:7 
W20151203-17:09:54.347(-8)? (STDERR)  at /Users/me/Dev/Web/borderApp/.meteor/local/build/programs/server/app/borderApp.js:67:4 
W20151203-17:09:54.347(-8)? (STDERR)  at /Users/me/Dev/Web/borderApp/.meteor/local/build/programs/server/boot.js:242:10 
W20151203-17:09:54.347(-8)? (STDERR)  at Array.forEach (native) 
W20151203-17:09:54.347(-8)? (STDERR)  at Function._.each._.forEach (/Users/me/.meteor/packages/meteor-tool/.1.1.10.1evms9b++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11) 
W20151203-17:09:54.347(-8)? (STDERR)  at /Users/me/Dev/Web/borderApp/.meteor/local/build/programs/server/boot.js:137:5 

这是我使用了前端的HTML:

<head> 
    <title>Todo List</title> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> 
</head> 

<body> 
    <div class="form-group"> 
    <header> 
     <h1>Todo List</h1> 

     <form class="new-task"> 
     <input class="input-lg form-control" type="text" name="text" placeholder="Type to add new tasks" /> 
     </form> 
    </header> 

<H3> 
{{> hello}} 
</h3> 

    </div> 
</body> 

<template name="hello"> 
    <p>{{borders}}</p> 
</template> 

<template name="button"> 
<button type="submit" class="btn btn-danger">Find Borders &rarr;</button> 
</template> 

回答

0

从技术上讲,你得到的错误是因为你写了session.set(varhere)而不是Session.set('nameofsessionvar',newvalueforsessionvar)。但是,即使如此,它也无法工作,因为会话变量对于客户端而言是全局可用的,而不是服务器。

尝试改变:

//session.set(countryCode) 
Meteor.call("getID", countryCode, function(error, results) { 
... 
} 

和:

//session.get(countryCode) 
//var url = "http://restcountries.eu/rest/v1/alpha/"+countryCode; 
getID: function (countrycode) { 
     var url = "http://restcountries.eu/rest/v1/alpha/"+countryCode; 
} 
0

你可能误解了MeteorSession不同的框架。在MeteorSession仅在客户端使用,而不是在服务器上,请参考:http://docs.meteor.com/#/full/session

对于你的问题,你可以通过COUNTRYCODE作为参数Meteor.call

Meteor.call("getID", countryCode, function(error, results) { 
    console.log(results.content); //results.data should be a JSON object 
    }); 

你的服务器:

if (Meteor.isServer) { 
    Meteor.methods({ 
     getID: function (countryCode) { 
     var url = "http://restcountries.eu/rest/v1/alpha/"+countryCode; 
      this.unblock(); 
      HTTP.get(url, {timeout:30000}, function(error, response) { 
      var respJson = JSON.parse(response.content); 
      console.log(respJson) 

      // Insert a task into the collection 
      borders.insert({ 
       text: respJson["borders"], 
       createdAt: new Date() // current time 
      }); 
      // Clear form 
      event.target.text.value = ""; 
      }); 
     } 
    }); 
}