【问题标题】:How do I deal with AWS and Session variable in Meteor?如何处理 Meteor 中的 AWS 和 Session 变量?
【发布时间】:2014-08-09 14:03:24
【问题描述】:

我正在为一个与学校相关的项目开发一个小型应用程序,但我无法理解我做错了什么。我正在使用 npm-apac 模块(使用meteor-npm),它工作正常(将结果返回到服务器端控制台),但它并没有在客户端做我希望它做的事情。相关代码如下:

Meteor 方法(服务器)

Meteor.startup ->

    Meteor.methods

        "isbnLookup": (isbn) ->

            console.log isbn

            opHelper.execute 'ItemLookup',
                SearchIndex: 'Books',
                ResponseGroup: 'Medium,Images',
                IdType: 'ISBN',
                ItemId: isbn
            , (res) ->
                console.log res
                return res

Meteor 事件(点击表单提交 ISBN# 的按钮)

Template.addbook.events

    'click #isbn-btn': ->

        theISBN = $("#isbn").val().trim()
        console.log theISBN
        Meteor.call 'isbnLookup', theISBN, (err, res) ->
            if (err)
                console.log(err.reason)
            else
                console.log 'SUCCESS!'
                Session.set('isbnResult', res)

用于从 Meteor.call 中获取结果的模板助手,据说存储在 Session 中

Template.addbook.helpers
    amazonSection: ->
        Session.get('isbnResult')

调用上述帮助程序的页面部分,我认为它会显示上述帮助程序的(未格式化,json junk)结果:

<div class="col-lg-6">
    {{ amazonSection }}
</div>

我真的不知道我做错了什么。我对处理 Meteor.call 的东西有点陌生,而且由于它使用 NPM 模块和其他东西,我认为我正在按照我应该的方式处理回调。当我检查服务器控制台时,console.log 确实是从 Amazon 输出数据,所以我知道 APAC 工作正常,显然我无法将该结果传输到客户端进行显示。

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js amazon-web-services meteor


    【解决方案1】:

    opHelper.execute 是异步的,因此isbnLookup 将始终返回undefined。此相关问题中讨论了解决方案。

    Meteor: Calling an asynchronous function inside a Meteor.method and returning the result

    或者,如果您将查找结果存储在一个集合中,您将在查找完成时在集合中看到结果。

    Template.addbook.events 
    
      "click #isbn-btn": ->
    
        theISBN = $("#isbn").val().trim()
        Session.set "isbn", theISBN
    
        return  if Books.findOne(ItemId: theISBN)
    
        Meteor.call "isbnLookup", theISBN, (err, res) ->
          if err
            console.log err.reason
          else
            console.log "SUCCESS!"
          return
    
    
    Meteor.startup ->
    
      Meteor.methods isbnLookup: (isbn) ->
    
        opHelper.execute "ItemLookup",
          SearchIndex: "Books"
          ResponseGroup: "Medium,Images"
          IdType: "ISBN"
          ItemId: isbn
        , Meteor.bindEnvironment((res) ->
          Books.insert res
        )
    
    
    
    Template.addbook.helpers
      amazonSection: ->
        Books.findOne(ItemId: Session.get("isbn"))
    

    【讨论】:

    • 当我尝试使用此方法解决我的问题时,我收到以下错误:gist.github.com/misutowolf/1f816611e5b7d5268b8d
    • @MisutoWolf 我的错。使用 Meteor.bindEnvironment 包装 opHelper.execute 的回调。
    • 肯定会取得进展,现在通过亚太地区的 AWS 响应给我带来了麻烦。当前输出如下:(我添加了一行将响应转储到控制台(使用按钮)以检查 AWS 响应)gist.github.com/misutowolf/935697d7b969b48aedf8 应该注意应用程序不会崩溃,但 .insert() 不会不行。也许我必须通过解析响应来构建自己的 JSON,然后插入它?看起来 APAC 模块在返回的数据中粘贴了一堆“$”键?
    • 我将此标记为解决方案,因为它确实解决了我提出的问题。 .insert 的这个问题似乎是 APAC 的问题,而不是我正在处理的 async/Meteor.method 问题。不过,仍在尝试解决此问题。
    • @MisutoWolf 我认为你是对的。只需将您想要的响应字段复制到一个新对象中(使用有效的键!)并插入它。
    猜你喜欢
    • 2012-05-28
    • 2020-04-19
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    相关资源
    最近更新 更多