MEAN stack ng-upload-file - angularjs

I am currently using MEAN.js to create an app and I scaffolded a simple entity called Campaign. I would like each Campaign to have a picture associated. Therefore, I would like to change the CRUD interface to be able to upload a file to the back end.
I injected the ng-file-upload plugin to create the FE with Angular. On the Node.js side, I installed the multer plugin to help me save the file into a folder (e.g. ./uploads). The thing is that I do not quite get the flow and I was hoping for a suggestion.
Please, find below the view:
<section data-ng-controller="CampaignsController">
<div class="page-header">
<h1>New Campaign</h1>
</div>
<div class="col-md-12">
<form class="form-horizontal" data-ng-submit="create()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<button ng-file-select ng-model="token">Upload the token</button>
<div ng-file-drop ng-model="token" class="drop-box"
drag-over-class="{accept:'dragover', reject:'dragover-err', delay:100}"
accept="image/*">
Drop image file here
</div>
<div ng-no-file-drop>Image drop is not supported for this browser.</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
Then, the Angular controller action:
// Create new Campaign
$scope.create = function() {
// Create new Campaign object
var campaign = new Campaigns ({
name: this.name
});
$scope.$watch('token', function() {
$scope.upload = $upload.upload({
url: '/campaigns', //upload.php script, node.js route, or servlet url
method: 'POST', //Post or Put
headers: {'Content-Type': 'multipart/form-data'},
//withCredentials: true,
data: campaign, //from data to send along with the file
file: $scope.token, // or list of files ($files) for html5 only
//fileName: 'photo' // to modify the name of the file(s)
}).success(function (response, status) {
// Redirect after save
campaign.$save(function(response) {
$location.path('campaigns/' + response._id);
// Clear form fields
$scope.name = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
}
).error(function (errorResponse) {
$scope.error = errorResponse.data;
//$scope.error = errorResponse.data.message;
});
});
};
Finally, the Node.js controller portion:
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
multer = require('multer'),
Campaign = mongoose.model('Campaign'),
_ = require('lodash');
/**
* Create a Campaign
*/
exports.create = function(req, res) {
var campaign = new Campaign(req.body);
campaign.user = req.user;
multer({
dest: './uploads/'
});
campaign.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(campaign);
}
});
};
Right now, what happens is that - when I try to upload a file - the uploader does not wait for the file to be selected, but it sends the POST request immediately (why?). Moreover, I get a 400 response.
Any suggestion would be really appreciated!
Thanks
Cheers

I partially solved the problem.
This is the new view:
<section data-ng-controller="CampaignsController">
<div class="container">
<div class="page-header">
<h1>New Campaign</h1>
</div>
<div class="col-sm-12 col-md-4 col-md-offset-4">
<form class="form-horizontal" data-ng-submit="create(token)" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="token">Token</label>
<div class="controls">
<input type="file" id="token" ng-file-select ng-model="token"/>
<p class="help-block">The token file must be a squared .png or .jpg image.</p>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="submit" class="btn btn-default col-xs-12">
</div>
</div>
<div class="form-group">
<div data-ng-show="error" class="control alert alert-danger alert-dismissible" role="alert">
<span data-ng-bind="error"></span>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</section>
Then, the Angular controller action:
$scope.create = function(token) {
// Create new Campaign object
var campaign = new Campaigns ({
name: this.name
});
$scope.upload = $upload.upload({
url: '/campaigns',
method: 'POST',
headers: {'Content-Type': 'multipart/form-data'},
//withCredentials: true,
data: {
campaign: JSON.stringify(campaign)
},
file: token,
//fileName: 'token' // to modify the name of the file
}).success(function (response, status) {
// Redirect after save
$location.path('campaigns/' + response._id);
// Clear form fields
$scope.name = '';
$scope.token = '';
}
).error(function (errorResponse) {
$scope.error = errorResponse.data;
}
);
};
I am now using node multiparty for the Node.js controller:
exports.create = function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
//res.writeHead(200, {'content-type': 'text/plain'});
//res.end(util.inspect({fields: fields, files: files}));
var file = files.file[0];
var contentType = file.headers['content-type'];
var tmpPath = file.path;
var extIndex = tmpPath.lastIndexOf('.');
var extension = (extIndex < 0) ? '' : tmpPath.substr(extIndex);
// uuid is for generating unique filenames.
//var fileName = uuid.v4() + extension;
var fileName = tmpPath;
var destPath = 'uploads/' + fileName;
// Server side file type checker.
if (contentType !== 'image/png' && contentType !== 'image/jpeg') {
fs.unlink(tmpPath);
return res.status(400).send({
message: 'Unsupported file type'
});
}
fs.rename(tmpPath, destPath, function(err) {
if (err) {
return res.status(400).send({
message: 'Image is not saved'
});
}
fs.unlink(tmpPath, function() {
if (err) {
return res.status(400).send({
message: 'Impossible to delete temp file'
});
}
});
console.log(destPath);
//return res.jsonp(destPath);
});
var campaign = new Campaign(JSON.parse(fields.campaign[0]));
campaign.user = req.user;
campaign.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(campaign);
}
});
});
};
I still get an error, but I do not think is related with the file upload. What do you think?
/home/maurizio/Workspace/bdf-v1/node_modules/mongoose/lib/utils.js:413
throw err;
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
at ServerResponse.header (/home/maurizio/Workspace/bdf-v1/node_modules/express/lib/response.js:592:10)
at ServerResponse.send (/home/maurizio/Workspace/bdf-v1/node_modules/express/lib/response.js:144:12)
at ServerResponse.jsonp (/home/maurizio/Workspace/bdf-v1/node_modules/express/lib/response.js:301:15)
at Promise. (/home/maurizio/Workspace/bdf-v1/app/controllers/campaigns.server.controller.js:67:9)
at Promise. (/home/maurizio/Workspace/bdf-v1/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)

Using res.status(400).send... or res.jsonp() will send data back to the client starting with the headers. Your script has these statements falling through, but the subsequent ones cannot be executed since data has already been sent to the client.
The returns you have will end the execution of the method they're invoked in, but the script will just continue to the next method where it will encounter another express send(). In your case, fs.rename will send() the 400, but will encounter another send() when it reaches the campaign.save method where it will throw the error.
Take your calls of return res.status(400).send(), and instead set the message as a string variable, and make the res.status(400).send() call in your final conditional statement if an error is present.
Essentially, make sure a send() or jsonp() invocation can only be made once in your script.

Related

Cannot pass data to the as.net mvc controller from $http in angularJS

I am Getting null value in action method when i am pass data from angular js to action method. data is coming up to $scope.custmodel and i debug and see it hit to the AddCutomer method but data is null. can anyone help me to fix this issue
Admin.js code
var app = angular.module("adminmdl", [])
app.controller("admincontroller", function ($scope,AdminService) {
$scope.Action = 'Add';
$scope.data = {
cus_code: '',
cus_name: ''
}
$scope.savecu = function () {
AdminService.saveCustomerDdetails($scope.cusmodel).then(function (data) {
if (data != null) {
alert('Insert successfully');
} else {
alert('error in inerting data');
}
});
}
})
.factory("AdminService", function ($http) {
var fact = {};
fact.saveCustomerDdetails = function (d) {
return $http({
url: '/Admin/AddCutomer',
method: 'POST',
data: JSON.stringify(d),
headers: { 'content-type': 'application/json' }
});
};
return fact;
});
ASP MVC Method
[HttpPost]
public JsonResult AddCutomer(Customer customer) {
te.Customers.Add(customer);
te.SaveChanges();
string message = "Success";
return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
html code
<form class="form-horizontal" method="post" action="#" name="basic_validate" id="basic_validate" novalidate="novalidate">
<div class="control-group">
<label class="control-label">Customer Code</label>
<div class="controls">
<input type="text" ng-model="cusmodel.Customercode" name="required" id="required" >
</div>
</div>
<div class="control-group">
<label class="control-label">Customer Name</label>
<div class="controls">
<input type="text" ng-model="cusmodel.Customername" name="name" id="name" >
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" value="Save" ng-click="savecu()" class="btn btn-success">
<input type="submit" value="Clear" class="btn btn-success" />
</div>
</div>
add [FromBody] attribute to the controller as shown below
public JsonResult AddCutomer([FromBody] Customer customer)

Image upload and retrieve using Node Express 4 MongoDB

I have created a simple login and signup using MEAN stack languages. I have a profile page where user can edit his info like name, address, etc. I am able to store and retrieve input type "text" fields. How can i add image upload feature and retrieve uploaded images? Im new to mean stack so can anyone please help me? Thanks.
My server.js is this
require('rootpath')();
var express = require('express');
var app = express();
var session = require('express-session');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var config = require('config.json');
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({ secret: config.secret, resave: false, saveUninitialized: true }));
// use JWT auth to secure the api
app.use('/api', expressJwt({ secret: config.secret }).unless({ path: ['/api/users/authenticate', '/api/users/register'] }));
// routes
app.use('/login', require('./controllers/login.controller'));
app.use('/register', require('./controllers/register.controller'));
app.use('/app', require('./controllers/app.controller'));
app.use('/api/users', require('./controllers/api/users.controller'));
// make '/app' default route
app.get('/', function (req, res) {
return res.redirect('/app');
});
// start server
var server = app.listen(3000, function () {
console.log('Server listening at http://' + server.address().address + ':' + server.address().port);
});
HTML to save and view saved data looks like this
<h1>My Account</h1>
<div class="form-container">
<form method="post" >
<div class="form-group">
<label for="firstName">First name</label>
<input type="text" id="firstName" class="form-control" ng-model="vm.user.firstName" required />
</div>
<div class="form-group">
<label for="lastName">Last name</label>
<input type="text" id="lastName" class="form-control" ng-model="vm.user.lastName" required />
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" class="form-control" ng-model="vm.user.username" required />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" class="form-control" ng-model="vm.user.password" />
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" id="password" class="form-control" ng-model="vm.user.address" />
</div>
<div class="form-group">
<button class="btn btn-primary" ng-click="vm.saveUser()">Save</button>
<button class="btn btn-danger" ng-click="vm.deleteUser()">Delete</button>
</div>
</form>
</div>
<!-- View Data -->
<div class="col-md-12">
<h1>Hi {{vm.user.firstName}}!!</h1>
<p><strong>User Name :</strong> {{vm.user.username}} </p>
<p><strong>Full Name : </strong> {{vm.user.firstName}} {{ vm.user.lastName}}</p>
<p><strong> Your address is : </strong> {{vm.user.address}}</p>
</div>
and app.js looks like this
(function () {
'use strict';
angular
.module('app')
.controller('Account.IndexController', Controller);
function Controller($window, UserService, FlashService) {
var vm = this;
vm.user = null;
vm.saveUser = saveUser;
vm.deleteUser = deleteUser;
initController();
function initController() {
// get current user
UserService.GetCurrent().then(function (user) {
vm.user = user;
});
}
function saveUser() {
UserService.Update(vm.user)
.then(function () {
FlashService.Success('User updated');
})
.catch(function (error) {
FlashService.Error(error);
});
}
function deleteUser() {
UserService.Delete(vm.user._id)
.then(function () {
// log user out
$window.location = '/login';
})
.catch(function (error) {
FlashService.Error(error);
});
}
}
})();
and user.service.js which is used to update data to db is
function updateUser() {
// fields to update
var set = {
firstName: userParam.firstName,
lastName: userParam.lastName,
username: userParam.username,
address: userParam.address,
};
// update password if it was entered
if (userParam.password) {
set.hash = bcrypt.hashSync(userParam.password, 10);
}
db.users.update(
{ _id: mongo.helper.toObjectID(_id) },
{ $set: set },
function (err, doc) {
if (err) deferred.reject(err.name + ': ' + err.message);
deferred.resolve();
});
}
To build the image upload feature, You need to add functionality to both the frontend and backend.
You need to have a custom image upload directive or component in the angularjs and Some kind of upload handler package in the Express Server as well.
Keeping all this in mind, Google will surely lead you to best package available for you.
One code sample can be found here : https://gist.github.com/keithics/bf0e13feaee5631fa936b7b203029cd4
Other: https://github.com/nervgh/angular-file-upload
There are huge number of libraries available for the same.

angular js: Unable to validate a textarea

fellas!
I'm trying to validate a text area upon clicking a link. Thing is, It's not coming inside a form. And when a user clicks a link, content entered in the text area should be posted (saved to database).
I wrote a validation for that. And unfortunately, it's not working and I'm able to make blank posts. I'm trying to prevent blank post posting. I have recreated the form and the controller in a fiddle. Link I'll provide down. but before that, take a look at my html and js code.
HTML
<div ng-app="myApp" ng-controller="myMap">
<div class="post-textarea" ng-class="{ 'has-error': vm.currentPost.content.$dirty && vm.currentPost.content.$error.required }">
<textarea class="form-control" rows="3" ng-model="vm.currentPost.content" required></textarea>
<a ng-click="vm.addPost(vm.currentPost.content,vm.currentPost.$valid)">Clik to Post and validate</a>
</div>
</div>
JavaScript
angular.module('myApp', [])
.factory('myService', function($http) {
var baseUrl = 'api/';
return {
postCurrentPost: function(newPost) {
var dataPost = {
newPost: newPost
};
return $http({
method: 'post',
url: baseUrl + 'postCurrentPost',
data: dataPost,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}
};
})
.controller('myMap', function(myService, $http, $scope) {
var vm = this;
var userObject;
// Add a post
vm.addPost = function(newPost, isValid) {
if (isValid) {
var currentPost = vm.currentPost;
currentPost.content = ""; //clear post textarea
myService.postCurrentPost(newPost).success(function(data) {
vm.posts = data;
});
} else {
alert('Validation not working');
}
};
//Check validation
$scope.getError = function(error, name) {
if (angular.isDefined(error)) {
if (error.required && name == 'vm.currentPost.content') {
return "This field is required";
}
}
}
});
And here's a FIDDLE.
Name of the form input element should be used instead of model value.
https://docs.angularjs.org/api/ng/directive/form
<div ng-app="myApp" ng-controller="myMap">
<form name="formContent">
<div class="post-textarea" ng-class="{ 'has-error': formContent.content.$dirty && formContent.content.$error.required }">
<textarea name='content' class="form-control" rows="3" ng-model="vm.currentPost.content" required></textarea>
<a ng-click="vm.addPost(vm.currentPost.content,vm.currentPost.$valid)">Clik to Post and validate</a>
</div>
</form>
Content is dirty: {{formContent.content.$dirty}}
<br>
Content has required: {{formContent.content.$error.required}}
</div>

how to redirect to other page when request is made using angularjs to spring controller

#MY Login page code snippet
--------------------------------
<div class="login-container">
<form ng-submit="onLogin()" name="form" class="pure-form login-form"
ng-controller="LoginCtrl as vm">
<fieldset class="pure-group">
<legend>Log In</legend>
<label for="inputUsername" class="sr-only">Username</label> <input type="text" id="inputUsername" class="form-control"
placeholder="Username" required autofocus ng-model="vm.username">
<label for="inputPassword" class="sr-only">Password</label> <br>
<input type="password" id="inputPassword" class="form-control"
placeholder="Password" required ng-model="vm.password">
</fieldset>
<button type="submit"
class="pure-button pure-input-1 pure-button-primary">Log In</button>
<div class="alert alert-danger" role="alert" ng-show="errorMsg">{{errorMsg}}</div>
</form>
</div>
<div id="content" class="col-lg-10 col-sm-10">
<div class="row">
<div class="box col-md-12">
<div class="box-inner"></div>
</div>
</div>
</div>
# login.js
-----------------
angular.module('loginApp', [])
.controller('LoginCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.onLogin = function () {
var encodedString = '?username=' +
encodeURIComponent($scope.vm.username) +
'&password=' +
encodeURIComponent($scope.vm.password);
var url = "http://localhost:8080/servicesummary/employees/view"+encodedString;
$http({
method: 'GET',
url: url
})
.success(function(data) {
//alert(JSON.stringify(data));
var container = 'box-inner';
$('.' + container).html(response);
})
.error(function(data) {
$scope.$error = 'Unable to submit form';
})
};
}]);
# Spring Controller-
--------------------------
#RequestMapping(value = "/view", method = RequestMethod.GET, produces = "application/json")
public ModelAndView getEmployeeInJSON(#RequestParam("username") String username, #RequestParam("password") String password) {
final ModelAndView mv = new ModelAndView();
try {
AppUserDTO appUserdb = userService.getObject(username);
if (null != appUserdb && (appUserdb.getPassword() != null)) {
String decodedPass = UtilityService.decode(appUserdb.getPassword());
if (decodedPass.equals(password)) {
System.out.println("Database Match.............");
mv.setViewName(VIEWNAME_HOME);
} else {
mv.setViewName(VIEWNAME_LOGIN);
}
} else {
mv.addObject("uNameNotValid", false);
mv.setViewName(VIEWNAME_LOGIN);
}
} catch (Exception e) {
logger.error("ERROR handleGetLogin() {}", e);
System.out.println("Exception is occured.....");
return new ModelAndView("redirect:/home-exception");
}
return mv;
}
Hi i have user input username and password which i trying to validate from database using spring controller (TestController.java) from angularjs. i am bale to call controller using login.js written, but not able to process the ModelAndView object returned using angularjs. please see my login.js for reference.
You can update your $http method as below
$http({
method: 'GET',
url: url
})
.success(function(data) {
//alert(JSON.stringify(data));
$window.location.href = 'SuccessUrl';
})
.error(function(data) {
$scope.$error = 'Unable to submit form';
})

Sharepoint 2013 App AngularJS: lookup field causing error when adding to list "A type named 'SP.Data.TeamsListItem' could not be resolved by the model

I have a Players List with a lookup field for Team Name linked to the Teams List.
When trying to add or update the Players List I am getting the error
"A type named 'SP.Data.TeamsListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type.".
The guid for the players list is '16a03b71-07ea-49c4-ab14-419b51047119'.
The guid for the teams list is '2a2916c2-22fb-4588-bca1-83479a4d85b2'.
This is the contents of data field when the error occurs:
<pre>
"{\"__metadata\":{\"type\":\"SP.Data.PlayersListItem\"},\"Title\":\"Bill\",\"PlayerPosition\":\"Goalie\",\"Birthdate\":\"2/21/1999\",\"TeamName\":{\"__metadata\":{\"id\":\"Web/Lists(guid'2a2916c2-22fb-4588-bca1-83479a4d85b2')/Items(1)\",\"uri\":\"https://app-2971fdcf9ede6a.rawtecapps.com/sites/raiders/AngularCRM/_api/Web/Lists(guid'2a2916c2-22fb-4588-bca1-83479a4d85b2')/Items(1)\",\"etag\":\"\\\"1\\\"\",\"type\":\"SP.Data.TeamsListItem\"},\"Id\":1,\"Title\":\"aa\",\"ID\":1,\"$$hashKey\":\"object:176\"},\"Season\":\"2015\"}"
</pre>
Definition of my Lookup field.
<pre>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field
ID="{7853bbf2-9fd7-4f85-aca8-269d8f967650}"
Name="TeamName"
DisplayName="Team Name"
Type="Lookup"
List="Lists/Teams"
ShowField="Title"
Required="FALSE"
Group="Custom Site Columns">
</Field>
</Elements>
</pre>
Content Type for Teams List:
<pre>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"><ContentType ID="0x0100A379177A3533447D9B6546708F7DFD2D" Name="Teams" Group="Custom Content Types" Description="Teams Content Type" Inherits="FALSE" Version="0">
<FieldRefs>
<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" DisplayName="Team Name" Required="TRUE" Name="Title" />
<FieldRef ID="{fce82853-3f44-4cc9-8f71-eb6506be0e9e}" DisplayName="Team City" Required="FALSE" Name="TeamCity" />
</FieldRefs>
</ContentType>
</pre>
playersnew.html page:
<pre>
<div class="form-horizontal">
<fieldset>
<div class="form-group">
<label for="txtID" class="col-lg-2 control-label">ID:</label>
<div class="col-lg-6">
<input id="txtID" type="text" readonly class="form-control" ng-model="player.ID">
</div>
</div>
<div class="form-group">
<label for="txtPlayerName" class="col-lg-2 control-label">Player Name:</label>
<div class="col-lg-6">
<input id="txtPlayerName" type="text" class="form-control" ng-model="player.Title">
</div>
</div>
<div class="form-group">
<label for="txtPlayerPosition" class="col-lg-2 control-label">Player Position:</label>
<div class="col-lg-6">
<input id="txtPlayerPosition" type="text" class="form-control" ng-model="player.PlayerPosition">
</div>
</div>
<div class="form-group">
<label for="txtBirthdate" class="col-lg-2 control-label">Birthdate:</label>
<div class="col-lg-6">
<input id="txtBirthdate" type="text" class="form-control" ng-model="player.Birthdate">
</div>
</div>
<div class="form-group">
<label for="txtTeamName" class="col-lg-2 control-label">Team Name:</label>
<div class="col-lg-6" ng-controller="TeamDropDownController">
<select name="teams" ng-model="player.TeamName" ng-options="team.Title for team in teams">
<option ng-repeat="team in teams" value="{{team.Id}}">{{team.Title}}</option>
</select>
</div>
</div>
<div class="form-group">
<label for="txtSeason" class="col-lg-2 control-label">Season:</label>
<div class="col-lg-6">
<input id="txtSeason" type="text" class="form-control" ng-model="player.Season">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2">
<input id="cmdSave" type="button" class="button" value="Save" data-ng-click="addPlayer()" />
</div>
</div>
</pre>
Angular JS (playersnew Controller):
><pre>
app.controller('playersnewController',
function ($scope, $location, wingtipCrmService) {
$scope.player = {};
$scope.player.Title = "";
$scope.player.PlayerPosition = "";
$scope.player.Birthdate = "";
$scope.player.TeamName = "";
$scope.player.TeamName.Id = "";
$scope.player.TeamName.Title = "";
$scope.player.Season = "";
$scope.addPlayer = function () {
var playerName = $scope.player.Title;
var playerPosition = $scope.player.PlayerPosition;
var birthdate = $scope.player.Birthdate;
var teamName = $scope.player.TeamName;
var teamNameId = $scope.player.TeamName.Id;
var teamNameTitle = $scope.player.TeamName.Title;
var season = $scope.player.Season;
wingtipCrmService.addPlayer(playerName, playerPosition, birthdate, teamName, teamNameId, teamNameTitle ,season)
.success(function (data) {
$location.path("/playershome");
});
}
} );
</pre>
Angular JS (service for addplayer):
> <pre>
service.addPlayer = function (PlayerName,PlayerPosition,Birthdate,TeamName,TeamNameId,TeamNameTitle,Season) {
var restQueryUrl = "../_api/web/lists/getByTitle('Players')/items";
var playerData = {
__metadata: { "type": "SP.Data.PlayersListItem" },
Title: PlayerName,
PlayerPosition: PlayerPosition,
Birthdate: Birthdate,
TeamName: TeamName,
Season: Season
};
var requestBody = JSON.stringify(playerData);
return $http({
method: 'POST',
url: restQueryUrl,
contentType: "application/json;odata=verbose",
data: requestBody,
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": requestDigest,
"content-type": "application/json;odata=verbose"
}
});
}
</pre>
Angular JS (TeamDropDownController and service):
<pre>
app.controller('TeamDropDownController',
function ($scope, wingtipCrmService) {
wingtipCrmService.getDropDownTeams().success(function (data) {
$scope.teams = data.d.results;
})
});
</pre>
<pre>
service.getDropDownTeams = function () {
var restQueryUrl = "../_api/web/lists/getByTitle('Teams')/items/" +
"?$select=ID,Title";
return $http({
method: 'GET',
url: restQueryUrl,
headers: { "Accept": "application/json; odata=verbose" }
})
}
</pre>
I am new to developing sharepoint apps and Angular JS. I have been trying to figure out a solution for this issue for over a week and in the process I have learned a great deal.
I am using TeamDropDownController to load the select statement with team names and then using playersnewcontroller to add new player record.
The add player javascript works until I added the code to include the TeamName field. I am afraid that the way I am loading the dropdown with a controller is causing a problem with the playersnewController.
The error message:
"A type named '{ListItemEntityTypeFullName}' could not be resolved by the
model. When a model is available, each type name must resolve to a
valid type.".
commonly occurs when List Item entity with the specified name could not be found. In your case it means there is no entity with name SP.Data.PlayersListItem.
The following endpoint allows to properly determine list item entity name:
Uri: /_api/lists/getbytitle('{List Title}')?$select=ListItemEntityTypeFullName
Method: GET
The following jQuery example demonstrates how to dynamically determine entity type name while creating list item:
function createListItem(webUrl,listTitle,itemProperties)
{
var requestUri = webUrl + "/_api/lists/getbytitle('" + listTitle + "')?$select=ListItemEntityTypeFullName";
return executeJson({url:requestUri}).then(function(data){
var requestUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
itemProperties['__metadata'] = {'type': data.d.ListItemEntityTypeFullName};
return executeJson({url: requestUri, method: 'POST', data: itemProperties});
});
}
var properties = {'Title':'John Doe'};
createListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',properties)
.done(function(data)
{
console.log('List item has been created');
})
.fail(function(error){
console.log(JSON.stringify(error));
});
where
function executeJson(options)
{
method = options.method || 'GET';
headers = options.headers || {};
headers["Accept"] = "application/json;odata=verbose";
if(method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions =
{
url: options.url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if (typeof options.data != 'undefined') {
ajaxOptions.data = JSON.stringify(options.data);
}
return $.ajax(ajaxOptions);
}
To fix my issue I added the player record without filling the TeamName lookup field. Then immediately performed an update of the player record with the TeamName field information.

Resources