使用mongoose throws更新文档范围错误

时间:2014-06-02 16:44:20

标签: node.js mongodb mongoose

我在node.js中有一个带有mongoose ODM的更新函数,如下所示:

var dataObj = req.body;
var id = dataObj.id;  
var updateDetails = function (data) {
            models.bookingConf.update({_id: id}, data, function (err, rows) {
                console.log(err);
                if (!err) {
                    res.send(dataObj);
                }
            });
        };

这总是抛出:

  

[RangeError:超出最大调用堆栈大小]

当我做console.log(数据)时,我得到了这个:

{ contact: 
   { _id: 538ca3e612f259a82a000002,
     company: 'Some company',
     contactGroup: 538249fd7c8bc4e94c000003,
     firstName: 'name',
     lastName: 'lastname',
     email: 'test@asdf.com',
     address: '',
     city: '',
     state: '',
     zip: '',
     country: '',
     website: '',
     skypeHandle: '',
     bankAccountNumber: '',
     bankAccountName: '',
     __v: 0,
     directDial: { country: '', area: '', number: '' },
     mobile: { country: '', area: '', number: '' },
     fax: { country: '', area: '', number: '' },
     telephone: { country: '', area: '', number: '' },
     people: [] },
  reference: '',
  carrierReference: '',
  poNumber: '',
  carrier: '',
  vessel: '',
  flight: '',
  origin: '',
  loadingPort: '',
  destination: '',
  finalDestination: '',
  cutOffDate: '2014-06-02T16:17:00.000Z',
  departureDate: '2014-06-01T18:30:00.000Z',
  arrivalDate: '2014-06-01T18:30:00.000Z',
  earliestPickupDate: '2014-06-01T18:30:00.000Z',
  earliestReturnDate: '2014-06-01T18:30:00.000Z',
  latestReturnDate: '2014-06-01T18:30:00.000Z',
  items: [],
  __v: 0,
  date: '2014-06-01T18:30:00.000Z' }

编辑:完整代码

app.put('/api/booking-confirmation-document/*', ensureAuthenticated, function (req, res) {
        var dataObj = req.body;
        var id = dataObj.id;
        delete dataObj.id;
        var tasks = [];
        var updateDetails = function (data) {
            models.bookingConf.update({_id: id}, data, function (err, rows) {
                console.log(err);
                if (!err) {
                    res.send(data);
                }
            });
        };
        if (dataObj.contact) {
            tasks.push(function (callback) {
                models.contact.find({_id: dataObj.contact}, function (err, data) {
                    dataObj['contact'] = data[0];
                    callback(null);
                });
            });
        }
        if (dataObj.returnLocation) {
            tasks.push(function (callback) {
                models.contact.find({_id: dataObj.returnLocation}, function (err, data) {
                    dataObj['returnLocation'] = data[0];
                    callback(null);
                });
            });
        }
        if (dataObj.emptyPickupAddress) {
            tasks.push(function (callback) {
                models.contact.find({_id: dataObj.emptyPickupAddress}, function (err, data) {
                    dataObj['emptyPickupAddress'] = data[0];
                    callback(null);
                });
            });
        }
        if (tasks.length > 0) {
            async.parallel(tasks, function (err, callback) {
                updateDetails(dataObj);
            });
        }
        else {
            updateDetails(dataObj);
        }
    });

数据库模式

module.exports.bookingConf = Schema({
    date: { type: Date, default: Date.now },
    cutOffDate: Date,
    departureDate:Date,
    arrivalDate:Date,
    latestReturnDate:Date,
    earliestReturnDate:Date,
    earliestPickupDate:Date
}, {strict: false});

0 个答案:

没有答案