angularjs - Creating fields in CRUD model MEAN.js -
im starting mean.js framework , i´m trying create first app. im creating app crud module generated yeoman (yo meanjs:crud-module).
the app simple form 2 fields. "name" , "image". name title of image, , image url of image.
my problem can´t insert in mongo collection "photos" both fields form. field "name" generated default crud module generator inserted correctly in collection. im doing wrong?
photo.server.model.js
'use strict'; /** * module dependencies. */ var mongoose = require('mongoose'), schema = mongoose.schema; /** * photo schema */ var photoschema = new schema({ name: { type: string, default: '', required: 'introduce el nombre de la imagen', trim: true }, image: { type: string, default: '', required: 'introduce la url de la imagen', trim: true }, created: { type: date, default: date.now }, user: { type: schema.objectid, ref: 'user' } }); mongoose.model('photo', photoschema);
photo.server.controller.js
'use strict'; /** * module dependencies. */ var path = require('path'), mongoose = require('mongoose'), photo = mongoose.model('photo'), errorhandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')), _ = require('lodash'); /** * create photo */ exports.create = function(req, res) { var photo = new photo(req.body); photo.user = req.user; photo.save(function(err) { if (err) { return res.status(400).send({ message: errorhandler.geterrormessage(err) }); } else { res.jsonp(photo); } }); }; /** * show current photo */ exports.read = function(req, res) { // convert mongoose document json var photo = req.photo ? req.photo.tojson() : {}; // add custom field article, determining if current user "owner". // note: field not persisted database, since doesn't exist in article model. photo.iscurrentuserowner = req.user && photo.user && photo.user._id.tostring() === req.user._id.tostring(); res.jsonp(photo); }; /** * update photo */ exports.update = function(req, res) { var photo = req.photo; photo = _.extend(photo, req.body); photo.save(function(err) { if (err) { return res.status(400).send({ message: errorhandler.geterrormessage(err) }); } else { res.jsonp(photo); } }); }; /** * delete photo */ exports.delete = function(req, res) { var photo = req.photo; photo.remove(function(err) { if (err) { return res.status(400).send({ message: errorhandler.geterrormessage(err) }); } else { res.jsonp(photo); } }); }; /** * list of photos */ exports.list = function(req, res) { photo.find().sort('-created').populate('user', 'displayname').exec(function(err, photos) { if (err) { return res.status(400).send({ message: errorhandler.geterrormessage(err) }); } else { res.jsonp(photos); } }); }; /** * photo middleware */ exports.photobyid = function(req, res, next, id) { if (!mongoose.types.objectid.isvalid(id)) { return res.status(400).send({ message: 'photo invalid' }); } photo.findbyid(id).populate('user', 'displayname').exec(function (err, photo) { if (err) { return next(err); } else if (!photo) { return res.status(404).send({ message: 'no photo identifier has been found' }); } req.photo = photo; next(); }); };
photo.client.controller.js
(function () { 'use strict'; // photos controller angular .module('photos') .controller('photoscontroller', photoscontroller); photoscontroller.$inject = ['$scope', '$state', '$window', 'authentication', 'photoresolve']; function photoscontroller ($scope, $state, $window, authentication, photo) { var vm = this; vm.authentication = authentication; vm.photo = photo; vm.error = null; vm.form = {}; vm.remove = remove; vm.save = save; // remove existing photo function remove() { if ($window.confirm('are sure want delete?')) { vm.photo.$remove($state.go('photos.list')); } } // save photo function save(isvalid) { if (!isvalid) { $scope.$broadcast('show-errors-check-validity', 'vm.form.photoform'); return false; } // todo: move create/update logic service if (vm.photo._id) { vm.photo.$update(successcallback, errorcallback); } else { vm.photo.$save(successcallback, errorcallback); //console.log(vm.photo.image); } function successcallback(res) { $state.go('photos.view', { photoid: res._id }); } function errorcallback(res) { vm.error = res.data.message; } } } }());
form-photo.client.view.html
<section> <div class="page-header"> <h1>{{vm.photo._id ? 'edit photo' : 'nueva imagen'}}</h1> </div> <div class="col-md-12"> <form name="vm.form.photoform" class="form-horizontal" ng-submit="vm.save(vm.form.photoform.$valid)" novalidate> <fieldset> <div class="form-group" show-errors> <label class="control-label" for="name">nombre</label> <input name="name" type="text" ng-model="vm.photo.name" id="name" class="form-control" placeholder="name" required> <div ng-messages="vm.form.photoform.name.$error" role="alert"> <p class="help-block error-text" ng-message="required">el nombre de la foto es requerido</p> </div> </div> <div class="form-group" show-errors> <label class="control-label" for="image">url</label> <input name="image" type="text" ng-model="vm.photo.image" id="image" class="form-control" placeholder="url" required> <div ng-messages="vm.form.photoform.image.$error" role="alert"> <p class="help-block error-text" ng-message="required">la url de la foto es requerido</p> </div> </div> <div class="form-group"> <button type="submit" class="btn btn-default">{{vm.photo._id ? 'update' : 'crear'}}</button> </div> <div ng-show="vm.error" class="text-danger"> <strong ng-bind="vm.error"></strong> </div> </fieldset> </form> </div> </section>
use link , see example, since
mean 'js'
uses
multer
to upload in 0.4.2 integrated if want u can generate , copy there , edit little bit since not compatible
Comments
Post a Comment