How to add username to a post

I have this:

Post.insert({content: content}); which obviously inserts the content and displays it in browser.

So now I want to display username too along with it.

I tried like this:

var username = Meteor.user().username;

Post.insert({
content: content,
username: username
});
But its not working. I also tried like this, still the insert does not happen.

Post.insert({
content: content,
username: Meteor.user().username
});

Not only username is not displayed it also not allow to insert the content as well. And no error whatsoever, just the insert doesn’t happen.

What other packages are you using? Is username even set in the Meteor.user() object?

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.

In post.js which is under lib folder outside server and client folder, I put this code:

export const Post = new Mongo.Collection('post');

Meteor.methods({
  addPost: function (content) {
    var username = Meteor.user().username;
    Post.insert({content: content, username: username});
  }
});

Under postsForm.js which is in client/template folder, I put the following code:

Template.postsForm.events({
  'submit form': function(event){
    event.preventDefault();
    var content = document.getElementById('content').value;
    Meteor.call('addPost', content);
    event.target.reset();
  }
});

under postsList.html in client/template folder, following code is present:

<template name = "postsList">

  <ul class = "post-list">
    {{#each posts}}
      <li class = "post">
        <div>{{content}}</div>
      <div>Author <a>Follow</a></div>
      </li>
    {{/each}}
  </ul>

</template>

Now the problem is insert doesn’t work and of course the word “Author” is displayed but when I replace it with {{username}} nothing is displayed.

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

The insecure package is removed. So do I need to provide allow/deny for the code to work?

Move the method so it only executes on the server

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 :slight_smile:

Are there any errors in the browser console or server log?

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

There you have it. Did you forget to import Post from the file you defined it in?

I did. That’s the first thing I checked :slight_smile: 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.

You need to post more code, especially the file that contains your method.

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.

I tried with import/export and done everything as you suggested. Still not working. No error on client or server side.

In that case, I’m with @jamgold: we need to see much more (all) of your code.

Do you have a git repo?

In import/both/post.js, I put this code:

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.