Mongoose:返回的错误是空对象

时间:2018-02-09 19:49:51

标签: node.js mongodb mongoose error-handling

我做错了什么? 尝试通过findOneAndUpdate保存新条目。

Offer.findOneAndUpdate(conditions,payload,{upsert:true,new: true},function(err, offer) {
    if(err){
            response.status(500).send({"message": "This is an error! Can not save _offer", "error":err, "payload":payload});
    }else{
            response.status(200).send(offer);
    }
});

但是我得到了错误500,错误= {}

  

{" message":"这是一个错误!无法保存   _offer""错误":{},"有效载荷" {"富":"杆"}}

我该怎么办?

2 个答案:

答案 0 :(得分:3)

发现问题:条件不符合架构:/但是来自mongo / mongoose的不良行为

错误:{}

var conditions = {
 foo: bar
};

解决方案:

var conditions = {
 foo: new ObjectId(bar._id)
};

需要在Object

上引用ObjectId

答案 1 :(得分:0)

我不确定Mongoose是否为我们提供了" findoneandupdate"你有

[ParallelisableProcess] Im built !
[DumbassWorker] Im built !
Job given to worker #0
Job given to worker #1
Job given to worker #2
Job given to worker #3
Launching a real task !
I'm starting a job !

Launching a real task !
I'm starting a job !

Launching a real task !
I'm starting a job !

All workers are busy ! 2 works pending.
All workers are busy ! 3 works pending.
All workers are busy ! 4 works pending.
All workers are busy ! 5 works pending.
All workers are busy ! 6 works pending.
All workers are busy ! 7 works pending.
All workers are busy ! 8 works pending.
All workers are busy ! 9 works pending.
[DumbassWorker] Im dying...
0 is waiting to die...
My job is done sir !.

Launching a real task !
I'm starting a job !

My job is done sir !.

Launching a real task !
I'm starting a job !

My job is done sir !.

Launching a real task !
I'm starting a job !

Launching a real task !
I'm starting a job !

My job is done sir !.

My job is done sir !.
My job is done sir !.

Launching a real task !
I'm starting a job !

Launching a real task !
I'm starting a job !


Launching a real task !
I'm starting a job !

My job is done sir !.

Launching a real task !
I'm starting a job !

My job is done sir !.

Launching a real task !
I'm starting a job !

My job is done sir !.

My job is done sir !.

Worker #My job is done sir !.
0 is killed !

1 is waiting to die...
My job is done sir !.

Worker #1 is killed !
2 is waiting to die...
Worker #2 is killed !
3 is waiting to die...
Worker #3 is killed !
I died
[ParallelisableProcess] Im dying...

相反,您可以尝试以下代码:

.save()

以下是Docs.

的链接

如果它不起作用,请尝试此操作,请指定条件和有效负载。