I’m following a tutorial, so as instructed, I installed accounts-ui and accounts-password packages and the above code works fine for the instructor but it doesn’t for me. The instructor is using meteor 1.2 and I’m using meteor 1.5, so is there anything else i need to import or something.
We would need to see more code to help you. In case you do post some of your code, please wrap it into tripple back-ticks so it gets formatted correctly.
So the method is being executed both on client and server. Unless you removed the insecure package and provided allow/deny rule it will fail on the client. On the server the insert will succeed, but there is no Meteor.user() on the server. I suggest to move the method into the lib/server directory and change it to
Meteor.methods({
addPost: function (content) {
var user = Meteor.users.findOne(this.userId);
var username = user ? user.username : 'Anonymous';
Post.insert({content: content, username: username});
}
});
I moved the method into server folder and put it in another .js file, but nothing happened, still can’t post from the client. Sorry I’m really new so be patient
No error on client side but on server the following error is thrown:
Exception while invoking method ‘addPost’ ReferenceError: Post is not defined
I20170619-21:05:32.485(5.5)? at [object Object].Meteor.methods.addPost (server/lib.js:5:5)
I20170619-21:05:32.485(5.5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1737:12)
I20170619-21:05:32.485(5.5)? at packages/ddp-server/livedata_server.js:719:19
I20170619-21:05:32.486(5.5)? at [object Object]..extend.withValue (packages/meteor.js:1122:17)
I20170619-21:05:32.486(5.5)? at packages/ddp-server/livedata_server.js:717:40
I20170619-21:05:32.486(5.5)? at [object Object]..extend.withValue (packages/meteor.js:1122:17)
I20170619-21:05:32.487(5.5)? at packages/ddp-server/livedata_server.js:715:46
I20170619-21:05:32.487(5.5)? at [object Object]._.extend.protocol_handlers.method (packages/ddp-server/livedata_server.js
I did. That’s the first thing I checked when I got the error, but that is not the case, because there was an earlier error which didn’t resolve until I added import statements.
I think you’re trying to mix two styles of Meteor coding: import/export and “Meteor Classic”. You should probably choose one and stick to it until you understand what’s happening.
The following is a huge over-simplification, but it should get you started.
Meteor Classic
Don’t use import/export (they didn’t exist originally). You put your collection in a file in `lib/’ and it looks like this:
Post = new Mongo.Collection('post');
and you just refer to Post in your client and server code. Meteor ensures that lib/ gets bundled into both.
import/export
Create an import/both/ folder and declare your collection in there:
export const Post = new Mongo.Collection('post');
Then, in every file which uses Post ensure you have import { Post } from '/imports/both/posts'; at the top.
Methods
If you’re using “Meteor Classic” and you want client-side simulation, put your method declaration in a file in your lib/ folder. Meteor will bundle it on client and server. If you don’t want client simulation, put that file in the server/ folder.
If you’re using import/export, put your method declaration in a file in your imports/both/ folder and import it wherever you want to use it - client and/or server files as appropriate.
export const Post = new Mongo.Collection('post');
Meteor.methods({
addPost: function (content) {
var username = Meteor.user().username;
Post.insert({content: content, username: username});
}
});
Alternatively I tried this too as @jamgold suggested.
export const Post = new Mongo.Collection('post');
Meteor.methods({
addPost: function (content) {
var user = Meteor.users.findOne(this.userId);
var username = user ? user.username : 'Anonymous';
Post.insert({content: content, username: username});
}
});
In client/template/postsForm.js
import { Post } from '/import/both/post.js';
Template.postsForm.events({
'submit form': function(event){
event.preventDefault();
var content = document.getElementById('content').value;
Meteor.call('addPost', content);
event.target.reset();
}
});
In client/template/postsList.js
import { Post } from '/import/both/post.js';
Template.postsList.helpers({
posts: function(){
return Post.find({}, {sort: {'_id': -1}});
}
});
No errors.
You seem to have no server-side import of post.js, so there will be no persistent storage. I suspect that’s your main problem.
@jamgold’s method code is corrrect. There is no Meteor.user() available in a server-side method - that’s another indicator that there’s no server-side import - you would have had an error on your server console.