【发布时间】:2019-09-07 16:43:47
【问题描述】:
我像往常一样开发了我的 node rest api,但这次它在 controller.js 文件中显示了一些无效错误。不需要猫鼬。当我在邮递员中点击 API 时,它给出的错误是:
{
"error": {
"message": "Tweets is not a constructor"
}
}
我什至更新了我的软件包,但似乎没有任何效果。这是我的 tweets.js 控制器的 sn-p:
const mongoose = require('mongoose');
const Tweets = require('../models/tweets');
exports.get_all_tweets = (req, res, next) => {
Tweets.find()
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
exports.create_tweets = (req, res, next) => {
const tweetbody = req.body;
tweetbody._id = mongoose.Types.ObjectId();
const tweet = new Tweets(tweetbody);
tweet
.save()
.then(docs => {
console.log(docs, 'Tweets');
res.status(200).json(docs);
})
.catch(err => {
console.log(err, 'error found');
res.status(500).json({
error:err
});
});
第一条 mongoose 行显示为空白,如屏幕截图所示: mongoose
tweets.js 模型:
const mongoose = require('mongoose');
const tweetSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
time: { type: String},
count: { type: Number}
});
module.export = mongoose.model('Tweets', tweetSchema);
【问题讨论】:
-
.exec()的目的是什么? -
@narayansharma91 它正在返回一个承诺