本文介绍了为什么Meteor抱怨集合的插入方法已经定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我为什么下面的代码抛出以下错误? :

 错误:已定义名为/ players / insert的方法



我刚开始使用Meteor和coffeescript,所以我可能会忽略一些简单的东西。



我的排行榜端口示例coffeescript:

  ### 
设置集合以包含播放器信息。在服务器上,
它由名为players的MongoDB集合支持。
###
Players = new Meteor.Collection(players)

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

Template.leaderboard.selected_name = - >
player = Players.findOne(Session.getselected_player)
player和player.name

Template.player.selected = - >如果Session.equals(selected_player,this._id)thenselectedelse''

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)
}

#在服务器启动时,如果数据库为空,请创建一些播放器。
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 )* 10)* 5})名称的名称

完整的堆栈跟踪如下: / p>

  [[[[[〜/ dev / meteor / leaderboard]]]]] 

:http:// localhost:3000 /

node.js:201
throw e; // process.nextTick错误或第一个tick时的'error'事件
^
错误:已在app / packages / livedata / livedata_server中定义了一个名为/ players / insert的方法
.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)
在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
退出代码:1

I 'm running Meteor version 0.4.0(8f4045c1b9)



感谢您的帮助!

解决方案

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


Can anyone tell me why the code below throws the following error? :

Error: A method named '/players/insert' is already defined

I'm new to Meteor and coffeescript so I may be overlooking something simple.

Here's my port of the leaderboard example to 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

The full stack trace is as follows:

[[[[[ ~/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

I'm running Meteor version 0.4.0 (8f4045c1b9)

Thanks in advance for assistance!

解决方案

You would also get this error, regardless of using coffeescript or plain javascript, if you duplicated your files. For example, copying your sources files to a subdirectory named Backup would produce this error, because Meteor merges files from subdirectories.

这篇关于为什么Meteor抱怨集合的插入方法已经定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:54