猫鼬保存失败而没有错误 [英] mongoose save fails without error

查看:88
本文介绍了猫鼬保存失败而没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,猫鼬保存方法存在问题,该方法失败但不会产生错误.

As the title suggests I'm having problems with mongoose save method, which fails but does not produce an error.

我实际上知道为什么失败,这取决于将userId字段标记为必填但未提供...但是我不知道为什么它不会引发错误.我已经用尽了google和stackoverflow在没有运气的情况下寻找类似的建议,所以将其开放给任何可以帮助我的人!

I actually know why it fails, which is down to the userId field being marked as required but not being provided... but I don't know why it doesn't throw an error. I've exhausted google and stackoverflow looking at similar suggestions with no luck, so throwing it open to anyone who can help me!

这是代码...

Model.js

var mongoose = require('mongoose');

var TimeSchema = new mongoose.Schema({
    client: String,
    matter: String,
    activity: String,
    tags: String,
    description: String,
    comments: [String],
    startTime: Date,
    startTimeUTC: Number,
    endTime: Date,
    endTimeUTC: Number,
    duration: Number, 
    durationRnd: Number,    
    durationUnits: Number,  
    billable: Boolean,
    rate: Number,
    total: Number,
    user: String,
    userId: { type: mongoose.Schema.ObjectId, required: true }
}, {safe: true});

mongoose.model("Time", TimeSchema);


Controller.js

exports.addTime = function (req, res) {

    console.log('Adding time: ' + JSON.stringify(req.body));
    var time = new Time(req.body);
    time.save(function (err) {
        if (err) { res.send({'error' : err}); }
        res.send(time);
    });
}

EDIT -要说明正在调用回调,请以下面的代码为例.

EDIT - To clarify the callback is being called, take the following code for example.

exports.addTime = function (req, res) {

    console.log('Adding time: ' + JSON.stringify(req.body));
    var time = new Time(req.body);
    console.log("time = " + time);
    // TODO user
    time.save(function (err) {
        if (err) { handleError(res, err); }
        console.log("ok");
        Time.findById(time._id, function (err, found) {
            console.log("found = " + found);
        });
        res.send(time);

});

}

这是控制台输出

Adding time: {"description":"test","client":"","matter":"","activity":"","rate":
"","startTime":"2013-11-30T19:58:43.000Z","startTimeUTC":"1385841523000","endTim
e":"2013-11-30T19:58:45.000Z","endTimeUTC":"1385841525000","startLocale":"19:58"
,"startTimeLocale":"19:58:43","endLocale":"19:58","endTimeLocale":"19:58:45"}
time = { description: 'test',
  client: '',
  matter: '',
  activity: '',
  rate: null,
  startTime: Sat Nov 30 2013 19:58:43 GMT+0000 (GMT Standard Time),
  startTimeUTC: 1385841523000,
  endTime: Sat Nov 30 2013 19:58:45 GMT+0000 (GMT Standard Time),
  endTimeUTC: 1385841525000,
  startTimeLocale: '19:58:43',
  endTimeLocale: '19:58:45',
  _id: 529a43750a366b6419000001,
  comments: [] }
ok
POST /api/times 200 14ms - 313b
found = null

推荐答案

多亏了罗伯特克(robertkelp),问题得以解决.

Problem solved, thanks to robertkelp.

这是我修改后的代码,以防万一,如果有人帮助的话,但似乎是因为我没有正确处理它而引发了错误.

Here's my revised code in case if ever helps anyone, but it appears the error was being thrown I just wasn't handling it correctly.

exports.addTime = function (req, res) {

    console.log('Adding time: ' + JSON.stringify(req.body));
    var time = new Time(req.body);
    time.save(function (err) {
        if (err) { 
            handleError(res, err);
        }
        else {
            res.send(time);
        }
    });
}

这篇关于猫鼬保存失败而没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆