问题
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