0

Hello i am new to sails and would like some help with this. I am using sails with sails-mongo adapter and on my controllers when i am querying for data it returns okay and i can log the data fine but when i am trying to return the response back the object is empty. I have tried differen type of res like res.send() res.status(200).send() res.json() res.ok(). This is an example controller method:

class UserController {

  /**
   * Create User
   * @param {*} req 
   * @param {*} res 
   * @returns {User|Error}
   */
  static async create (req, res) {
    var data = _.pick(req.body, ['username', 'password']);
    Object.assign(data, { status: false });
    try {
      await User.create(data).fetch();
      res.status(200).send('User created successfully.');
    } catch (error) {
      res.status(400).send(error.message);
    }
  }

  /**
   * Get all active users
   * @param {*} req 
   * @param {*} res
   * @returns {[]User|Error}
   */
  static async getActive (req, res) {
    try{
      const users = await User.find({ status: true });
      console.log(users);
      return res.status(200).json(users);
    } catch(error) {
      res.status(500).send(error.message);
    }
  }
}

module.exports = {
  create: UserController.create,
  getActive: UserController.getActive,
}

I can see the return on log but the response is like this:

[
    {}
]

2 Answers2

0

If your request comes from an Ajax Form, you have to use an Action instead of an Controller. Maybe you can try .json({user: user}) . Im also new to sails and i am already run into some strange errors and things in sails ^^. Maybe it will work, let me know.

0

If you're on a Mac and the files were copied, there's a possibility that there are some hidden attributes messing with your logic. I hunted a similar issue for hours but in the end what worked for me was deleting the affected model and typing the code manually. It doesn't make sense to me yet but it works, perhaps someone who knows what's going on could explain

G-John
  • 3
  • 2