【问题标题】:Error: Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency错误:警告:在循环依赖中访问模块导出的不存在属性“findOne”
【发布时间】:2021-03-28 11:47:38
【问题描述】:

这是我的代码,我不明白为什么会出现这个错误,我什至使用了命令 npm update 但似乎没有任何效果。

const router = require('express').Router();
const File = require('../models/file');
const User = require('../app');

router.get('/:uuid_d', async (req, res) => {
 // Extract link and get file from storage send download stream
 const file = await File.findOne({ uuid_d: req.params.uuid_d });
 const user = await User.findOne({ googleId: req.body.guploadBy});
 // Link expired
 if(!file) {
      return res.render('download', { error: 'Nothing Found'});
 }

 const response = await file.save();
 const response2 = await user.save();
 const filePath = `${__dirname}/../${file.path}`;
 file.downloads++;
 var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();

today = dd + '/' + mm + '/' + yyyy;
file.lastClick = today;
 file.save();

user.downloads++;
user.save();

 res.download(filePath);
});

module.exports = router;

这是我的错误

node:9868) 警告:在循环依赖中访问模块导出的不存在属性“findOne” (使用node --trace-warnings ... 显示警告的创建位置) (节点:9868)UnhandledPromiseRejectionWarning:TypeError:User.findOne 不是函数 在 C:\Users\Puneet\Documents\Dream Project\routes\download.js:8:26 在 processTicksAndRejections (internal/process/task_queues.js:93:5) (节点:9868)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1) (节点:9868)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

【问题讨论】:

  • app.js文件是否导出了一个名为findOne的函数?根据错误它没有。这个const User = require('../app');不应该是const User = require('../models/user');吗?我的意思是文件模型在/models/file 中,所以在模型文件夹中也有用户模型会很有意义。

标签: javascript node.js mongoose error-handling runtime-error


【解决方案1】:

对我来说,这解决了我的问题:

  1. yarn remove mongoose 或 npm uninstall mongoose --save
  2. yarn add mongoose 或 npm install mongoose --save

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 2021-06-11
    • 2021-08-21
    • 2021-02-13
    • 2021-02-19
    • 2021-05-16
    • 2021-03-01
    • 2022-06-13
    相关资源
    最近更新 更多