【发布时间】:2014-05-20 02:49:39
【问题描述】:
Error: TypeError: path must be a string 出现 500 内部服务器错误
浏览器控制台指向 ItemCtrl.js:35
console.log('Error: ' + data);
错误的堆栈跟踪如下所示:
TypeError:路径必须是字符串 在 Query.where (/Users/Name/Downloads/dev/v16/node_modules/mongoose/lib/query.js:593:11) 在 Function.where (/Users/Name/Downloads/dev/v16/node_modules/mongoose/lib/model.js:1040:18) 在模型。 (/Users/Name/Downloads/dev/v16/app/models/item.js:44:35) ...
似乎错误来自 /app/models/item.js 中的一行
特别是在else if 声明中..
因为 else if 被注释掉了,if 和 else 都有效..
// load mongoose since we need it to define a schema and model
var mongoose = require('mongoose');
var ItemSchema = mongoose.Schema({
title : String,
position: Number
});
// before validation starts, the number of Items is counted..afterwards, the position is set
ItemSchema.pre("validate", function(next) {
var doc = this;
// if 'position' is not filled in, fill it in
// not using !position because 0 might be a valid value
if(typeof doc.position !== "number") {
// count the number of Items *
// use mongoose.model to fetch the model because it is not compiled yet
mongoose.model("Item").count(function(err, num) {
// if there was an error, pass it to next()
if(err)
return next(err);
// set the position, then call next();
doc.position = num;
return next();
});
} else if(typeof doc.position == "number") {
console.log('yes, a number');
// check if there is an existing document with the same position
// use mongoose.model to fetch the model because the model is not compiled yet
mongoose.model("Item").where({_id: {$ne: doc._id}, position: doc.position}).count( function (err, count) {
// if there was an error, pass it to next()
if(err)
return next(err);
// if there is a doc with the SAME position, execute an update to MOVE DOWN all the $gte docs
if(count > 0) {
// use mongoose.model to fetch the model because the model is not compiled yet
mongoose.model("Item").update({position: {$gte: doc.position}}, {position: {$inc: 1}}, {multi: 1}, function(err, numAffected) {
console.log(numAffected);
// Call next() (with or without an error)
next(err);
});
} else {
// there are no docs that need to move down, so call next()
next();
}
});
} else {
// there is no need to count or update positions, so call next()
next();
}
});
module.exports = mongoose.model('Item', ItemSchema);
- 数据的路径是这样的:
- /public/views/CreateItem.html 中的表单字段
- 到 /public/js/controllers/ItemCtrl.js
- 到 /public/js/services/ItemService.js
- 到 /app/routes.js
- 到 /app/models/item.js
- 可以在localhost:8080/api/items json中查看数据
/public/views/CreateItem.html
<form>
<div class="form-group">
<!-- BIND THESE VALUES TO formData.value IN ANGULAR -->
<input type="text" class="form-control input-lg text-center" placeholder="Item" ng-model="formData.title">
<input type="number" class="form-control input-lg text-center" placeholder="0" ng-model="formData.position">
</div>
<!-- createItem() WILL CREATE THE NEW ITEM VIA ANGULAR THROUGH NODE API TO MONGOOSE -->
<button type="submit" class="btn btn-primary btn-lg" ng-click="createItem()">Add Item</button>
</form>
/public/js/controllers/ItemCtrl.js
angular.module('ItemCtrl', [])
// inject the Item service.factory into our controller
.controller('ItemController', function($scope, $http, Items, isEmptyObjectFilter) {
$scope.formData = {};
// CREATE ==================================================================
// when submitting the add form, send the text to the node API
$scope.createItem = function() {
// validate the formData (using our exentions.js .filter) to make sure that something is there..if form is empty, nothing will happen
if (!isEmptyObjectFilter($scope.formData)) {
// call the create function from our service (returns a promise object)
Items.create($scope.formData)
// if successful creation, call our get function to get all the new items
.success(function(data) {
$scope.formData = {}; // clear the form so our user is ready to enter another
$scope.items = data; // assign our new list of items
})
.error(function(data) {
console.log('Error: ' + data);
});
}
};
});
/public/js/services/ItemService.js
angular.module('ItemService', [])
// super simple service
// each function returns a promise object
.factory('Items', function($http) {
return {
get : function() {
return $http.get('/api/items');
},
create : function(itemData) {
return $http.post('/api/items', itemData);
}
}
});
/app/routes.js
// load the item model
var Item = require('./models/item');
// expose the routes to our app with module.exports
module.exports = function(app) {
// create item and send back all items after creation
app.post('/api/items', function(req, res) {
console.log(req.body.position);
// create an item, information comes from AJAX request from Angular
Item.create({
title : req.body.title,
position : req.body.position
}, function(err, item) {
if (err)
res.send(err);
// get and return all the items after you create another
Item.find(function(err, items) {
if (err)
res.send(err)
res.json(items);
});
});
});
};
【问题讨论】:
-
尝试
{$inc: {position: 1}},而不是{position: {$inc: 1}},假设第44行是触发错误的.update()语句。 -
@RikkusRukkus 谢谢,会试试看.. 第 44 行是
mongoose.model("Emotion").where({_id: {$ne: doc._id}, position: doc.position}).count( function (err, count) {
标签: javascript node.js angularjs mongodb mongoose