Node.js Prosime模型到控制器节点
我不明白为什么模型中的承诺不在控制器中返回数据 型号:Node.js Prosime模型到控制器节点,node.js,mongodb,Node.js,Mongodb,我不明白为什么模型中的承诺不在控制器中返回数据 型号: const getDb = require('../util/database').getDB; class Product{ constructor(title,price,description,imageUrl) { this.title = title; this.price = price; this.description = description; this.imageUrl
const getDb = require('../util/database').getDB;
class Product{
constructor(title,price,description,imageUrl) {
this.title = title;
this.price = price;
this.description = description;
this.imageUrl = imageUrl;
}
save(){
const db = getDb();
return db.collection('products')
.insertOne(this)
.then(result => {
console.log(result);
})
.catch(err =>{
console.log(err);
});
}
static fetchAll(){
const db = getDb();
return db.collection('products')
.find()
.toArray()
.then(products =>{
console.log(products);
})
.catch(err =>{
console.log(err);
});
}
}
module.exports = Product;
和控制器:
exports.getProducts = (req, res, next) => {
Product.fetchAll()
.then(products => {
res.render('shop/product-list', {
prods: products,
pageTitle: 'All Products',
path: '/products'
});
})
.catch(err => {
console.log(err);
});
};
在控制器中,静态方法fetchAll()未定义返回
我像使用框架一样使用MongoDB和Expressjs
谢谢你的支持
编辑-
问题是没有返回的日期,所以它的工作
static fetchAll(){
const db = getDb();
return db
.collection('products')
.find()
.toArray()
.then(products =>{
console.log(products);
***return products;***
})
.catch(err =>{
console.log(err);
});
}
}