Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. /* CREATE a new */
  2. router.post('/', upload.single('file'), function(req, res) {
  3. const phone = new Phone({
  4. name: req.body.name,
  5. brand: req.body.brand,
  6. image: `/uploads/${req.file.filename}`,
  7. specs: JSON.parse(req.body.specs) || []
  8. });
  9.  
  10. phone.save((err) => {
  11. if (err) {
  12. return res.send(err);
  13. }
  14.  
  15. return res.json({
  16. message: 'New Phone created!',
  17. phone: phone
  18. });
  19. });
  20. });
  21.  
  22. import { Component, OnInit } from '@angular/core';
  23. import { FileUploader } from 'ng2-file-upload';
  24. import { Router, ActivatedRoute } from '@angular/router';
  25.  
  26. @Component({
  27. selector: 'app-add-phone',
  28. templateUrl: './add-phone.component.html',
  29. styleUrls: ['./add-phone.component.css']
  30. })
  31.  
  32. export class AddPhoneComponent implements OnInit {
  33. uploader: FileUploader = new FileUploader({
  34. url: `/phones/`
  35. });
  36.  
  37. newPhone = {
  38. name: '',
  39. brand: '',
  40. specs: []
  41. };
  42.  
  43. feedback: string;
  44.  
  45. constructor() { }
  46.  
  47. ngOnInit() {
  48. this.uploader.onSuccessItem = (item, response) => {
  49. this.feedback = JSON.parse(response).message;
  50. };
  51.  
  52. this.uploader.onErrorItem = (item, response, status, headers) => {
  53. this.feedback = JSON.parse(response).message;
  54. };
  55. }
  56.  
  57. addSpec(spec) {
  58. this.newPhone.specs.push(spec);
  59. }
  60.  
  61. submit() {
  62. this.uploader.onBuildItemForm = (item, form) => {
  63. form.append('name', this.newPhone.name);
  64. form.append('brand', this.newPhone.brand);
  65. form.append('specs', JSON.stringify(this.newPhone.specs));
  66. };
  67.  
  68. this.uploader.uploadAll();
  69. }
  70. }
Add Comment
Please, Sign In to add comment