Problem on inserting data via javascript console

Hello;

I’ve created this collection:

PlayersList = new Mongo.Collection(‘players’);

When I try to insert data via chrome javascript console I get this error:

insert failed: Method ‘/players/insert’ not found

I write this in javascript console:

PlayersList.insert({ name: “Bob”, score: 0 });

Where is the problem?

please inform me.

thanks,
Saeed

If you’ve removed the insecure package from your app, client side writes are denied by default

Though going by the error you probably aren’t running the code that creates the collection on the server, make sure the new Mongo.Collection(‘players’); line is run on both server and client

2 Likes

Thanks for your reply.

How can I ensure code runs on server and client?

Use method, it runs on server and client: https://guide.meteor.com/methods.html

Just ensure that your const PlayersList = new Mongo.Collection('players'); line is put in a file containing your server code (like server/main.js) and also a file containing your client code (like client/main.js).

If you’re using import syntax, you’ll do this slightly differently, but the basic principle is to ensure that any code which needs to run on client and server is present on client and server.

2 Likes

Is there any simpler alternative to insert data to Mongodb? Like SQL databases?

You can use pretty much any database you want. If it’s got an NPM repo, you’re good to go.

As a long time SQL user, I would say that nothing comes easier than Meteor’s integration with MongoDB.

2 Likes

I’m trying to learn meteor from a tutorial. Every time I try to work interact with mongo from console panel I face a new problem and it waste my time!!:sleepy:

1 Like

Have you tried the official tutorial? It definitely works!

As for loading on server and client, can you post your project’s folder/file structure?

I’m try to learn meteor from METEOR YOUR FIRST APPLICATION.
It’s a pdf book from meteortips.com.
Here come my project:
https://drive.google.com/open?id=1ZWV1uWtgbR_UehXnOQj2SSPpAavN1Bna

It’s leaderboard project from meteor.

thanks

Looking at your code, it is because your collection is only defined on the client and not on the server.

Currently the line:

PlayersList = new Mongo.Collection('players');

Is only in /client/main.js, which means that it’s not being run on the server.

Simply add the same line into /server/main.js and it should insert fine

Even better make a both or lib folder for your database Collections:

/both/collections.js
/client/main.js
/server/main.js

Then add your collection to both/collections.js

PlayersList = new Mongo.Collection('players');

Because the variable has been set without using var, it will be globally scoped and available in all your other files. (This is considered bad practice for larger apps, but is fine for this example. It’s worth learning the import/export syntax later on too)

With that done (and with the insecure package installed), you can just use PlayersList directly in client/main.js

PlayersList.insert({ name: “Bob”, score: 0 });
1 Like

You mean I should declare PlayersList variable in 3 places?
Both, Server, Client. Is this correct?

Here comes a question:
If this is a Global variable, Why we need to declare it in more than one place?

Another question:
Where should I write the rest of code in Server or Client?

Thanks,

Both = Server + Client

So, no - only in one place.

1 Like

You mean I create a folder with name of both and place a javascript page with name of collections inside it and this will handles both server and client.

IS IT CORRECT?

1 Like

yes that is correct.