Why does Meteor complain that an insert method for

2019-04-26 20:37发布

问题:

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!

回答1:

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.



回答2:

This appears to be a configuration issue with coffeelint (installed globally with npm).

I originally installed coffeelint to check that my coffeescript code was correct and had no errors.

I installed coffeelint as per the instructions with:

sudo npm install -g coffeelint

coffeelint worked fine when run stand-alone against .coffee files.

However, when running any Meteor project with coffeescript package added I got the above error.

On a whim, I thought the error might be due to conflict with my exisiting node install.

I decided to uninstall coffeelint first with:

sudo npm uninstall -g coffeelint

and then deleted the previously meteor-generated leaderboard.js file.

After re-starting meteor the coffeescript example above worked as expected without errors.



回答3:

try moving (ie copying and deleting the original )

Players = new Meteor.Collection("players")

one time below if Meteor.is_client and another time below if Meteor.is_server

I don't know exactly why, as I'm new to Meteor too, but that worked for me, I assume that the server side needs it's own reference,as well as the client, although declaring outside the scope should do the same (maybe a bug, remember they're still at 0.5.0 preview , which makes me think you might wanna upgrade and try some new smart packages that are with the new version, it looks like you're using 0.4), but when the files in my server wouldn't recognize anything I defined the root directory of meteor (which pushes these files to both client and server), I defined the server's own reference, and I got the same error, and until I moved the 'public' declaration of the reference to give the server and the client each their own copy, nothing worked.

Hopefully that helps...