无需刷新即可进行多次插入

时间:2015-02-20 23:47:05

标签: javascript angularjs node.js mongodb

所以我有一个非常基本的应用程序正在使用MEAN堆栈。我正在提交表单数据并通过节点插入mongoLab但由于某种原因我不能在没有刷新的情况下发送两批数据。如果我尝试发送两个不同批次的数据,我会收到重复的密钥错误MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error

我对此很陌生,所以我不确定发生了什么。我知道Mongo不允许两个相同的字段为空,但所有数据都在更改并在提交时进行说明。这是我的代码:

app.controller('HistoryController', function($scope, $filter, $http){
    $scope.usersHistories = masterHistory;
    $scope.users = users;
    $scope.formatDate = function(date, format) {
        return $filter('date')(date, format);
    };

    $http.get('/api/history').
        success(function(data, status, headers, config){
            //var masterHistory = data.toJSON();
            console.log(data);
        //  masterHistory.push(data);
        });

/*  function appendToBSheet (block) {
        masterHistory.push(
        {
            transDate: block.date,
            directObject: block.user,
            transAmount: block.cost,
            transType: block.type,
            transLocale: block.location
        }
    );
    }*/

    $scope.formData = {};
    $scope.submit = function(){
        $http({
            method: 'POST',
            url: '/api/transactions',
            data: $.param($scope.formData),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
            .success(function(newTrans, status, headers, config){
                console.log(newTrans);
                //if(!newTrans.success) {
                //  $scope.errorName = newTrans.errors.name;
                //  $scope.errorLocation= newTrans.errors.location;
                //} else {
                    $scope.message = newTrans.message;
                //appendToBSheet(newTrans);
                $("form").trigger("reset");

                //}
            });
    };      
}); 

节点API

app.post('/api/transactions', parseUrlencoded, function(req, res){
...
  var transaction = {   };
var date = new Date();
var dd = date.getDate();
var mm = date.getMonth() + 1; //January is 0
var yy = date.getFullYear();

if (dd<10) {
    dd = "0" + dd;
}

if (mm<10) {
    mm = "0" + mm;
}

var HH = date.getHours();
var MM = date.getMinutes();
var timeHalf;


if (HH > 12) {
    timeHalf = "pm";
    HH = HH - 12;
} else {
    timeHalf = "am";
}

if (MM < 10) {
    MM = "0" + MM;
} else {
    MM=MM;
}

  date = yy + "-" + mm + "-" + dd;
  time = HH + ":" + MM + timeHalf;

  var newTrans = req.body;
  transaction["transDate"] = date
  transaction["transTime"] = time;  
  transaction["directObject"] = newTrans.name;
  transaction["transAmount"] = newTrans.cost;
  transaction["transType"] = "credit";
  transaction["transLocale"] = newTrans.location;    
  transaction["UserID"] = 2;


  var transColl = db.collection("transactions");

修改

  transColl.insert(transaction, function(err, result){
    if (err) throw err;
    res.status(200).json(result); //sending result after insert

  });

console.log(newTrans);

SCHEMA

{
"_id": "id",
"Date": "transDate",
"Time": "transTime",
"Recipient": "directObject",
"Type": "transType",
"Location": "transLocale",
"Verifier": "UserID"
}

1 个答案:

答案 0 :(得分:0)

我需要为你的mongo表看到你的模型Schema,但这可能是你的问题:

transaction["UserID"] = 2;

由于您正在对一个应该是唯一的db值进行硬编码,因此会导致重复的密钥问题。

请发布您的mongo表的模型定义,以便我详细说明

修改:还请发布 req.body