node.js - 猫鼬错误:检测到循环依赖
问题描述
我编写了一个使用Google Cloud Video Intelligence分析视频的服务
我用猫鼬将分析结果保存到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)
...
我尝试过的解决方案:
{autoIndex: false}
在 db 配置中添加。
mongoose.connect(process.env.DB_CONNECTION, {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, autoIndex: false });
retryWrites=true
从 Mongo URI 结构中移除。(我的连接 URI 中还没有那个参数)
所以,我认为问题的根源在于我正在保存整个测试结果,但我没有其他选择。我需要按原样保存。
我愿意接受各种建议。
解决方案
正如我所猜测的那样,问题在于cyclic dependency
对象中有一个来自谷歌的对象。
在我同事的帮助下:
然后由于 JSON.stringify() 将对象更改为简单类型:字符串、数字、数组、对象、布尔值,因此无法使用 stringify 存储对对象的引用,然后解析您破坏了 stringify 无法转换的信息。
另一种方法是知道哪个字段持有循环引用,然后取消设置或删除该字段。
我找不到哪个字段,cycylic dependency
所以我使用 IJSON.stringfy()
并JSON.parse()
删除它。
let videoAnnotiations = JSON.stringify(operationResult.annotationResults[0]);
videoAnnotiations = JSON.parse(videoAnnotiations);
推荐阅读
- angular - ngx-datatable 在对 Angular 6 的行详细信息表进行排序时给出“rxjs_1.fromEvent 不是函数”
- r - SAS 和 R 中的二项式检验 - 不同的结果
- vb.net - 如何运行 CMD 命令并将其隐藏
- php - php 5.3 和 7.2 中的 foreach 循环
- python - 确定 Flask 服务器何时准备好接受请求
- multithreading - 使用多线程编写 tfrecord 并不像预期的那样快
- wso2 - 如何在 wso2esb 4.9.0 中修补和重新编译axis2_1.6.1.wso2v14.jar
- angular - 表格中的 ng-Bootstrap Datepicker 弹出窗口未正确显示
- c - 在 STM32F101 控制器中,由于复位导致通信中断,I2C BUSY 标志置位。恢复机制也失败了
- php - 即使使用标头('Access-Control-Allow-Origin:*'),php api rest也不接受cors请求;