Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CREATE a new */
- router.post('/', upload.single('file'), function(req, res) {
- const phone = new Phone({
- name: req.body.name,
- brand: req.body.brand,
- image: `/uploads/${req.file.filename}`,
- specs: JSON.parse(req.body.specs) || []
- });
- phone.save((err) => {
- if (err) {
- return res.send(err);
- }
- return res.json({
- message: 'New Phone created!',
- phone: phone
- });
- });
- });
- import { Component, OnInit } from '@angular/core';
- import { FileUploader } from 'ng2-file-upload';
- import { Router, ActivatedRoute } from '@angular/router';
- @Component({
- selector: 'app-add-phone',
- templateUrl: './add-phone.component.html',
- styleUrls: ['./add-phone.component.css']
- })
- export class AddPhoneComponent implements OnInit {
- uploader: FileUploader = new FileUploader({
- url: `/phones/`
- });
- newPhone = {
- name: '',
- brand: '',
- specs: []
- };
- feedback: string;
- constructor() { }
- ngOnInit() {
- this.uploader.onSuccessItem = (item, response) => {
- this.feedback = JSON.parse(response).message;
- };
- this.uploader.onErrorItem = (item, response, status, headers) => {
- this.feedback = JSON.parse(response).message;
- };
- }
- addSpec(spec) {
- this.newPhone.specs.push(spec);
- }
- submit() {
- this.uploader.onBuildItemForm = (item, form) => {
- form.append('name', this.newPhone.name);
- form.append('brand', this.newPhone.brand);
- form.append('specs', JSON.stringify(this.newPhone.specs));
- };
- this.uploader.uploadAll();
- }
- }
Add Comment
Please, Sign In to add comment