function addFlight(req, res) {
Airline.create({
name: req.param('name'),
email: req.param('email'),
office: req.param('office'),
phone: req.param('phone')
}, function airlineCreated(err, newAirline) {
if (err) {
console.log('Error: ' + err);
return res.negotiate(err);
} else {
console.log('uploading file');
req.file('file').upload({
maxBytes: 10000000,
dirname: '../../assets/images/'
}, function whenDone(err, uploadedFiles) {
if (err) {
console.log(err);
return res.negotiate(err);
}
if (uploadedFiles.length === 0) {
console.log("No file");
return res.badRequest('No file was uploaded');
}
Airline.update(newAirline.id, {
logoUrl: require('util').format('%s/airline/logo/%s', sails.getBaseUrl(), newAirline.id),
logoFd: uploadedFiles[0].fd
}).exec(function (err) {
if (err) {
return res.negotiate(err);
console.log(err);
}
return res.ok();
});
});
}
});
}
I am currently learning the Sails framework and I encountered an issue with uploading and retrieving images. I followed the documentation and managed to get the uploading to work, as shown in the code above. Following that, here is the JSON format of my data after an image is uploaded.
{
"name": "imager",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef86af82c18c8082">[email protected]</a>",
"office": "img",
"phone": "24232",
"createdAt": "2016-04-04T09:06:35.404Z",
"updatedAt": "2016-04-04T09:06:35.438Z",
"id": 11,
"logoUrl": "http://localhost:1337/airline/logo/11",
"logoFd": "/Users/musaddiq/Desktop/Other proples companies/AirportAngular/airport/assets/images/6bd339f5-bc72-458c-b04c-6670bc1b59a5.jpg"
}
I have a list of individuals with images and a table view where I want to display all users with their logos. Below is the table view code. I am unsure how to proceed, as referencing the Sails documentation has not resolved my issue.
<div style="padding-top: 180px;" ng-app="airlineMod" ng-controller="airlineCtrl" class="container">
<div ng-init="airlineList()">
<table class="table table-responsive table-hover" ng-show="airlines.length">
<thead>
<tr>
<td>
Logo
</td>
<td>
Name
</td>
<td>
Office
</td>
<td>
Email
</td>
<td>
Phone Number
</td>
</tr>
</thead>
<tbody ng-repeat="airline in airlines">
<tr>
<td>
<img ng-src="{{airline.logoUrl}}" class="img-responsive img-circle" width="50" height="50">
</td>
<td>
{{airline.name}}
</td>
<td>
{{airline.office}}
</td>
<td>
{{airline.email}}
</td>
<td>
{{airline.phone}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
Here is the code I attempted to use to retrieve the images:
getAllFlights: function (req, res) {
Airline.find({}, function found(err, airlines) {
if (err) {
return res.negotiate(err);
}
console.log("Airlines found");
var SkipperDisk = require('skipper-disk');
var fileAdapter = SkipperDisk();
_.each(airlines, function (airline) {
fileAdapter.read(airline.logoFd)
.on('error', function (err) {
return res.serverError(err);
})
.pipe(res);
});
return res.send(airlines);
});
}
However, I keep encountering the error "fs.js:262 binding.open(pathModule._makeLong(path), TypeError: path must be a string".