猫鼬错误:检测到循环依赖

Mur*_*ran 3 mongoose mongodb node.js video-intelligence-api

我编写了一项使用Google Cloud Video Intelligence分析视频的服务

\n

我用mongoose将分析结果保存到MongoDB中

\n

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

\n
// Video.js\n\nconst mongoose = require(\'mongoose\');\n\nconst videoSchema = new mongoose.Schema({\n    analysis_progress: {\n        percent: { type: Number, required: true },\n        details: {}\n    },\n    status: {\n        type: String,\n        enum: [\'idle\', \'processing\', \'done\', \'failed\'],\n        default: \'idle\'\n    }\n});\n\nmodule.exports = mongoose.model(\'Video\', videoSchema);\n\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
\nfunction detectFaces(video, results) {\n   //Build query\n    let update = {\n        $set: {\n            \'analysis_results.face_annotations\': results.faceDetectionAnnotations // results is the the test result\n        }\n    };\n\n    Video.findOneAndUpdate({ _id: video._id }, update, { new: true }, (err, result) => {\n        if (!err)\n            return console.log("Succesfully saved faces annotiations:", video._id);\n        throw err // This is the line error thrown\n    });\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是我得到的错误:

\n
Error: cyclic dependency detected\n    at serializeObject (C:\\Users\\murat\\OneDrive\\Masa\xc3\xbcst\xc3\xbc\\bycape\\media-analysis-api\\node_modules\\bson\\lib\\bson\\parser\\serializer.js:333:34)\n    at serializeInto (C:\\Users\\murat\\OneDrive\\Masa\xc3\xbcst\xc3\xbc\\bycape\\media-analysis-api\\node_modules\\bson\\lib\\bson\\parser\\serializer.js:947:17)\n...\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试过的解决方案:

\n
    \n
  1. 添加{autoIndex: false}内部数据库配置。
  2. \n
\n
mongoose.connect(process.env.DB_CONNECTION, {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, autoIndex: false });\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. retryWrites=true从 Mongo URI 结构中删除。(我的连接 URI 中还没有该参数)
  2. \n
\n

所以,我认为问题的根源是我保存了整个测试结果,但我没有任何其他选择来做到这一点。我需要按原样保存。

\n

我愿意接受各种建议。

\n

Mur*_*ran 5

正如我所猜测的,问题是有一个cyclic dependency来自谷歌的对象。

在我同事的帮助下:

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

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

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

let videoAnnotiations = JSON.stringify(operationResult.annotationResults[0]);
videoAnnotiations = JSON.parse(videoAnnotiations);
Run Code Online (Sandbox Code Playgroud)