Why res.json() not working?

和自甴很熟 提交于 2019-12-12 03:29:26

问题


I'm new to sails.js and I'm trying to learn how to use models and controllers together. For that I have created the code given below. Now when I visit ....../corporate_info I see all the required fields in JSON format in PostMan. But it doesn't seem to be 'coming from' the res.json({}) I have defined in corporate_info.find() because the response doesn't have a property "data" in it. It should be actually {data : mydata} ?

blueprints.js

rest: false

Corporate_infoController

module.exports = {
    operations: function (req, res, next) {
        var params = req.body;
        Corporate_info.find(params, function (err, createdData) {
            if (err) {
                res.badRequest({
                    error: err
                });
            } else {
                return res.json({
                    status: "success",
                    data: createdData
                });
            }
        });

Corporate_info.js (model)

module.exports = {
  tableName: 'corporate_info',
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'integer',
      required: true,
      autoIncrement: true,
      primaryKey: true,
      size: 11
    },
    fname: {
      type: 'string',
      required: true,
      size: 100
    },
    lname: {
      type: 'string',
      required: true,
      size: 100
    },
    country_code: {
      type: 'string',
      required: true,
      size: 45
    },
    mobile: {
      type: 'string',
      required: true,
      unique: true,
      size: 100
    },
    email: {
      type: 'string',
      required: true,
      size: 100
    },
    address: {
      type: 'string',
      required: true,
      size: 100
    },
    userid: {
      type: 'integer',
      required: false,
      size: 11
    },
    imei_number: {
      type: 'string',
      required: false
    },
    fullname: function(){
      return "Fullname: "+fname+" "+lname;
    }
  }
};

Connections.js

...
 mysqlAdapter: {
    adapter: 'sails-mysql',
    host: 'localhost',
    user: 'root',
      password: '',
    database: 'rockcity_followme'
  }
...

models.js

...
connection: 'mysqlAdapter',
  migrate: 'safe'
...

来源:https://stackoverflow.com/questions/40761335/why-res-json-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!