2

I am newbie in sailsjs and try to learn by following the tutorial, I a getting 500 internal error while inserting post.

Here is my all files:

//DashboardController.js

newpost: function(req, res){
        var params = req.params.all();
        var userId = req.session.user_detail._id;

        //return false;
        Post.create({ title: params.heading, description: params.message, posted_by: params.userId }).done(function(error, post_created){
            if(err){

            }else{

                req.session.post_details = post_created;
                res.redirect('/dashboard');
            }
        });
    }

// Dashboard.js model

module.exports = {

  attributes: {
    title:{
         type:'string',
         required:true,
         columnName:'title'
    },
    description: {
         type:'text',
         columnName:'description'
    },
    posted_by: {
        type: 'integer',
        columnName: 'posted_by'
    }

  }
};

// ejs file

  <div class="col-sm-7">
        <h3>Post Feed Now</h3>
        <br>

        <div class="media"> 
          <div class="media-body">
          <form method="POST" action="/dashboard/newpost">
            <table class="table">
              <tr><td>Heading</td><td><input type="text" class="form-control" name="heading"></td></tr>
              <tr><td>Message</td><td><textarea name="message" class="form-control"></textarea></td></tr>
              <tr><td colspan="2"><input type="submit" class="btn btn-primary" value="Post Now"/></td></tr>
          </form>
            </table>

        </div>

and error in terminal

error: Sending 500 ("Server Error") response: 
 ReferenceError: Post is not defined
    at Object.module.exports.newpost (/var/www/html/sailsTemplate/api/controllers/DashboardController.js:21:3)
    at bound (/usr/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
    at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /usr/lib/node_modules/sails/lib/router/bind.js:187:7
    at alwaysAllow (/usr/lib/node_modules/sails/lib/hooks/policies/index.js:207:11)
    at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5) [ReferenceError: Post is not defined]

I want to create insert post in mongodb , is we have to create collection mongo db or code automatically create the collection while insert..

I don't know why i am getting this issue 500 internal server.

If anything required please asked me

Rahul Saxena
  • 465
  • 4
  • 15
  • Thanks for the question and I hope the answer helps. If you would like to discuss any Sails.js issues in realtime with other users, check out our Gitter page. https://gitter.im/balderdashy/sails – Travis Webb Aug 07 '15 at 21:30

1 Answers1

2

If your model file name is Dashboard.js replace Post.create by Dashboard.create

newpost: function(req, res){
        var params = req.params.all();
        var userId = req.session.user_detail._id;

        Dashboard.create({ title: params.heading, description: params.message, posted_by: params.userId }).done(function(error, post_created){
            if(err){

            }else{

                req.session.post_details = post_created;
                res.redirect('/dashboard');
            }
        });
    }

If you want Post as name for your model you have to rename your model file to Post.js

jaumard
  • 8,202
  • 3
  • 40
  • 63