Mongo中所有记录的增量字段?

时间:2014-04-06 20:14:09

标签: javascript node.js angularjs mongodb mongoose

错误实际上是在角度ItemController中引发的,错误:TypeError:path必须是一个字符串

$scope.createProduct = 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)
                Emotions.create($scope.formData)

                        // if successful creation, call our get function to get all the new emotions
                        .success(function(data) {
                                $scope.formData = {}; // clear the form so our user is ready to enter another
                                $scope.products = data; // assign our new list of emotions
                        })
                        .error(function(data) {
                                console.log('Error: ' + data);
                        });
        }
};

如果为了在新位置与现有位置匹配时将每条记录向下移动,则添加此其他内容会导致该500内部服务器错误..

 } 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("Product").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("Product").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();
        }
    });

我在Mongoose doc上有一个'position'字段,用于通过前端角度循环对结果进行排序。

var ProductSchema = mongoose.Schema({
    title : String,
    position: Number
});

对于没有输入“位置”的新文档,架构从“if”语句自动递增..

// before validation starts, the number of Products is counted
// afterwards, the position is set
ProductSchema.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 Products *
        // use mongoose.model to fetch the model..because it's not compiled yet
        mongoose.model("Product").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'语句,模式正确地存储了doc。

    } else {
        //  there is no need to count or update positions, so call next()
        next();
    }
});

module.exports = mongoose.model('Product', ProductSchema);
  

FRONTEND:输入类型=“数字”class =“form-control”占位符=“0”   NG-模型= “formData.position”

0 个答案:

没有答案