gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:45:15 24 4
gpt4 key购买 nike

当我点击新闻 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com