javascript - Nodejs Rest API : UnhandledPromiseRejectionWarning: TypeError: News. 查找不是函数

标签 javascript node.js mongodb mongoose

当我点击新闻 api 时,我似乎一直收到错误

UnhandledPromiseRejectionWarning: TypeError: News.find 不是函数

enter image description here

我阅读了有关 Promises 的内容,但无法解决这个问题。这是 Mongoose 的东西吗? 我该如何解决?

我的 Controller NewsController.js

const NewsController = {};
const News = require('../models/news.model');

// await indicates wait for the function to get over.
// we may also use Promises in its place
NewsController.getNews = async function(req, res) {
    const allnews = await News.find({}, function(err, ns) {
            assert.equal(err, null);
            res.json(ns);
        });
      //res.json(allnews);

  };
  // ES6 style
  NewsController.getSingleNews = async (req, res) => {

      const news = await News.findById(req.params.Id);
      res.json[news];
    };
    NewsController.createNews = async (req, res) => {
        const news = new News(req.body);
        await news.save();

        res.json[{
          'status': 'item saved successfully'
        }];
      };

      NewsController.deleteNews = async (req, res) => {
        await News.findByIdAndRemove(req.params.id);
        res.json[{
          'status': 'item deleted successfully'
        }]
      };

module.exports = NewsController;

我的模型 news.model.js

const mongoose = require('mongoose');


const {Schema}= mongoose;
const newsSchema = new Schema({
  title: {type: String, required:true},
  content:{type: String, required:true},
  author:{type: String},
  image:{type: String},
  source:{type: String}
});

mongoose.exports = mongoose.model('news', newsSchema);

最佳答案

问题是 mongoose.exports。应该是 module.exports 谢谢@AnthonyWinzlet

关于javascript - Nodejs Rest API : UnhandledPromiseRejectionWarning: TypeError: News. 查找不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54613096/

相关文章:

javascript - 数组 .length 属性为 JavaScript 中的 document.getElementsByClassName 返回 0

javascript - 后续 anchor 标记在 html/php 中不起作用

javascript - 检测在我的 Chrome 扩展之外生成的 DOM 更改

node.js - gulp 默认值,任务从未定义 : default, 请检查文档以了解正确的 gulpfile 格式

javascript - 在 Node.js 中使用 Jest 进行单元测试

MongoDB 将低基数字段添加到复合索引?

javascript - 使用 JavaScript 在 Asp.net mvc 中突出显示 html 文档表达式

oracle - db-oracle 没有刷新数据

python - 是否有使用Mongodb和Django的明确方法?

python - 如何从 Python Eve 应用程序中取消设置字段?