Mongoose错误:检测到循环依赖。

3
我编写了一个使用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中没有这个参数)

因此,我认为问题的根源在于我保存了整个测试结果,但我没有其他选择。我需要按原样保存。

我对各种建议都持开放态度。

1个回答

5

正如我所猜测的那样,问题在于来自谷歌的对象存在循环依赖。

在同事的帮助下:

由于JSON.stringify()将对象更改为简单类型:字符串、数字、数组、布尔对象,因此它无法存储对对象的引用,因此使用stringify然后解析会破坏不能转换的信息。

另一种方法是知道哪个字段持有循环引用,然后取消设置或删除该字段。

由于找不到哪个字段含有循环依赖,因此我使用了JSON.stringfy()JSON.parse()来移除它。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接