Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections Meteor为什么抱怨集合的插入方法已经定义?_Collections_Coffeescript_Meteor - Fatal编程技术网

Collections Meteor为什么抱怨集合的插入方法已经定义?

Collections Meteor为什么抱怨集合的插入方法已经定义?,collections,coffeescript,meteor,Collections,Coffeescript,Meteor,有人能告诉我为什么下面的代码会抛出以下错误吗 Error: A method named '/players/insert' is already defined 我对《流星》和《咖啡脚本》还不熟悉,所以我可能忽略了一些简单的东西 下面是我的coffeescript排行榜端口示例: ### Set up a collection to contain player information. On the server, it is backed by a MongoDB collection

有人能告诉我为什么下面的代码会抛出以下错误吗

Error: A method named '/players/insert' is already defined 
我对《流星》和《咖啡脚本》还不熟悉,所以我可能忽略了一些简单的东西

下面是我的coffeescript排行榜端口示例:

###
Set up a collection to contain player information. On the server,
it is backed by a MongoDB collection named "players."
###
Players = new Meteor.Collection("players")

if Meteor.is_client
  Template.leaderboard.players = ->
    Players.find({}, {sort: {score: -1, name: 1}})

  Template.leaderboard.selected_name = ->
    player = Players.findOne(Session.get "selected_player")
    player and player.name

  Template.player.selected = -> if Session.equals("selected_player", this._id) then "selected" else ''

  Template.leaderboard.events = {
    'click input.inc': ->
      Players.update(Session.get("selected_player"), {$inc: {score: 5}})
  }

  Template.player.events = {
    'click': ->
      Session.set("selected_player", this._id)
  }

# On server startup, create some players if the database is empty.
if Meteor.is_server
  Meteor.startup ->
    if Players.find().count() is 0
      names = [
                "Ada Lovelace"
                "Grace Hopper"
                "Marie Curie"
                "Carl Friedrich Gauss"
                "Nikola Tesla"
                "Claude Shannon"
              ]
      Players.insert({name: name, score: Math.floor(Math.random()*10)*5}) for name in names
完整堆栈跟踪如下所示:

[[[[[ ~/dev/meteor/leaderboard ]]]]]

Running on: http://localhost:3000/

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: A method named '/players/insert' is already defined
    at app/packages/livedata/livedata_server.js:744:15
    at Function.<anonymous> (app/packages/underscore/underscore.js:84:24)
    at [object Object].methods (app/packages/livedata/livedata_server.js:742:7)
    at new <anonymous> (app/packages/mongo-livedata/collection.js:111:13)
    at app/leaderboard.js:4:11
    at /Users/alex/dev/meteor/leaderboard/.meteor/local/build/server/server.js:109:21
    at Array.forEach (native)
    at Function.<anonymous> (/Users/alex/dev/meteor/leaderboard/.meteor/local/build/server/underscore.js:76:11)
    at /Users/alex/dev/meteor/leaderboard/.meteor/local/build/server/server.js:95:7
Exited with code: 1
[dev/meteor/leadboard][]]
运行于:http://localhost:3000/
node.js:201
抛出e;//process.nextTick错误,或第一次勾选时的“error”事件
^
错误:已定义名为“/players/insert”的方法
在app/packages/livedata/livedata_server.js:744:15
在功能上。(app/packages/下划线/下划线.js:84:24)
在[object object].methods(app/packages/livedata/livedata_server.js:742:7)
新建时(app/packages/mongolivedata/collection.js:111:13)
在app/leadboard.js:4:11
在/Users/alex/dev/meteor/leadboard/.meteor/local/build/server/server.js:109:21
at Array.forEach(本机)
在功能上。(/Users/alex/dev/meteor/leadboard/.meteor/local/build/server/underline.js:76:11)
在/Users/alex/dev/meteor/leadboard/.meteor/local/build/server/server.js:95:7
已退出,代码为:1
我正在运行Meteor版本0.4.0(8f4045c1b9)


提前感谢您的帮助

这似乎是coffeelint(与npm一起全球安装)的配置问题

我最初安装coffeelint是为了检查我的coffeescript代码是否正确且没有错误

我按照说明安装了coffeelint,包括:

sudo npm安装-g coffeelint

当对.coffeelint文件单独运行时,coffeelint工作正常

然而,当运行任何添加了coffeescript包的Meteor项目时,我得到了上述错误

一时兴起,我认为错误可能是由于与现有节点安装冲突

我决定首先卸载coffeelint,方法是:

sudo npm卸载-g coffeelint


然后删除了之前meteor生成的leadboard.js文件

重新启动meteor后,上面的coffeescript示例按预期运行,没有出现错误。

尝试移动(即复制和删除原件)

Players=newmeteor.Collection(“Players”)

如果Meteor.is\u客户机

如果Meteor.is_服务器

我不知道确切的原因,因为我也是Meteor的新手,但这对我来说很有效,我假设服务器端需要它自己的引用,以及客户端, 尽管在范围外声明也应该这样做(可能是一个bug,请记住它们仍然处于0.5.0预览状态,这让我认为您可能想升级并尝试一些新版本的智能软件包,看起来您使用的是0.4),但是当我的服务器中的文件无法识别任何东西时,我定义了meteor的根目录(它将这些文件推送到客户端和服务器),我定义了服务器自己的引用,我得到了相同的错误,直到我移动了引用的“public”声明,为服务器和客户端提供了各自的副本,什么都没用


希望这会有所帮助……

如果复制文件,无论使用coffeescript还是纯javascript,您都会遇到此错误。例如,将源文件复制到名为Backup的子目录会产生此错误,因为Meteor会合并子目录中的文件。

是否可以在某个地方发布完整的项目?我认为您的问题是定义集合两次。。也许你没有删除原始的.js文件?谢谢Jay-我发现了问题的根源并回答了我自己的问题。问题是由于与安装的coffeelint版本冲突(请参阅下面的详细信息)。谢谢,这解决了我的问题。我有一个SublimiteText插件配置错误,在.coffee上的每个保存操作都会生成.js文件。因此,我复制了客户端和服务器文件。谢谢,为我节省了大量时间。“然后删除了之前meteor生成的Leadboard.js文件”-正如其他人所说,此错误是由meteor两次实例化同一集合造成的。