【问题标题】:Passing JSON-like parameters in coffeescript在咖啡脚本中传递类似 JSON 的参数
【发布时间】:2013-04-01 02:25:31
【问题描述】:

我正在尝试使用通过 coffeescript 编写的 github API 做一些事情。我对它很陌生,我在这个块上遇到了麻烦。

getRepos: (user)->
    this.github.repos.getFromUser(
        user: user,
        type: "all",
        sort: "updated",
        direction: "desc"
        (err,res)-> console.log(JSON.stringify(res))
    )

目前正在编译为

 getRepos: function(user) {
  return this.github.repos.getFromUser({
   user: user
   }, {
   type: "all",
   sort: "updated",
   direction: "desc"
    }, function(err, res) {
   return console.log(JSON.stringify(res));
 });

而我不想在 user:user 后面加上额外的括号,那就是它应该是这样的:

getRepos: function(user) {
  return this.github.repos.getFromUser({
    user: user,
    type: "all",
    sort: "updated",
    direction: "desc"
  }, function(err, res) {
  return console.log(JSON.stringify(res));
});

coffeescript 缺少什么?

编辑:这是完整的代码

GitHubApi = require("node-github");

gitLoader = 
github: new GitHubApi({
    version: "3.0.0",
    timeout: 5000
}); 
authenticate: ->
    this.github.authenticate({
        type: "basic",
        username: username,
        password: password
    })
getRepos: (user)->
    this.github.repos.getFromUser(
        user: user,
        type: "all",
        sort: "updated",
        direction: "desc"
        (err,res)-> console.log(JSON.stringify(res))
    )
getFollowers: ->
    this.github.user.getFollowingFromUser(
        user: "example",
        (err, res)->console.log(JSON.stringify(res));
    )

编辑 2:认为它一定是某种间距问题。当我复制粘贴从编辑器复制的副本时,它编译正确。 = /

【问题讨论】:

  • 无法用您的代码复制它。 link
  • 我也是 - 检查您的间距,并注意您不需要逗号来分隔这些对象属性。您可以在这里进行实验:js2coffee.org/#coffee2js
  • 是的,如果我在 user: ... 前面添加一个额外的空格,我会得到不正确的输出
  • 嗯。设法让它工作。一定是某种间距问题...

标签: json coffeescript


【解决方案1】:

这看起来像是一个空白问题。

您还可以省略其他一些内容,因此您的代码如下所示:

getRepos: (user) ->
    @github.repos.getFromUser
        user: user
        type: "all"
        sort: "updated"
        direction: "desc"
        (err, res) -> console.log JSON.stringify res

【讨论】:

    【解决方案2】:

    我确定“用户:”之前有一个额外的空格(可能是一个制表符)

    如果您确保只有空格,它将按预期编译。

    getRepos: (user) ->
        this.github.repos.getFromUser(
            user: user,
            type: "all",
            sort: "updated",
            direction: "desc"
            (err,res) -> console.log(JSON.stringify(res))
    )
    

    【讨论】:

      猜你喜欢
      • 2013-06-11
      • 2019-03-03
      • 2013-01-09
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多