mongoose Error: cyclic dependency detected

mongoose Error: cyclic dependency detected

我写了一个分析视频的服务 Google Cloud Video Intelligence

然后我用 mongoose

将分析结果保存到 MongoDB

这是我使用的模型(我已经简化了一切以避免混淆):

// Video.js

const mongoose = require('mongoose');

const videoSchema = new mongoose.Schema({
    analysis_progress: {
        percent: { type: Number, required: true },
        details: {}
    },
    status: {
        type: String,
        enum: ['idle', 'processing', 'done', 'failed'],
        default: 'idle'
    }
});

module.exports = mongoose.model('Video', videoSchema);

当分析操作结束时,我调用下面的函数 运行 update 像这样:


function detectFaces(video, results) {
   //Build query
    let update = {
        $set: {
            'analysis_results.face_annotations': results.faceDetectionAnnotations // results is the the test result
        }
    };

    Video.findOneAndUpdate({ _id: video._id }, update, { new: true }, (err, result) => {
        if (!err)
            return console.log("Succesfully saved faces annotiations:", video._id);
        throw err // This is the line error thrown
    });
}

这是我得到的错误:

Error: cyclic dependency detected
    at serializeObject (C:\Users\murat\OneDrive\Masaüstü\bycape\media-analysis-api\node_modules\bson\lib\bson\parser\serializer.js:333:34)
    at serializeInto (C:\Users\murat\OneDrive\Masaüstü\bycape\media-analysis-api\node_modules\bson\lib\bson\parser\serializer.js:947:17)
...

我尝试过的解决方案:

  1. 在数据库配置中添加了 {autoIndex: false}
mongoose.connect(process.env.DB_CONNECTION, {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, autoIndex: false });
  1. 正在从 Mongo URI 结构中删除 retryWrites=true。 (我的连接 URI 中还没有那个参数)

所以,我认为问题的根源在于我正在保存整个测试结果,但我没有任何其他选择可以这样做。我需要原样保存。

我乐于接受各种建议。

正如我猜测的那样,问题是google给我的对象中有一个cyclic dependency

在同事的帮助下:

Then since JSON.stringify() changes an object into simple types: string, number, array, object, boolean it is not capable of storing references to objects therefor by using stringify and then parse you destroy the information that stringify cannot convert.

Another way would be knowing which field held the cyclic reference and then unsetting, or deleting that field.

我找不到哪个字段有 cycylic dependency,所以我用 JSON.stringfy()JSON.parse() 删除了它。

let videoAnnotiations = JSON.stringify(operationResult.annotationResults[0]);
videoAnnotiations = JSON.parse(videoAnnotiations);