[英]TypeError: Test.findAll is not a function callinf Mongoose Schema
這是我的架構
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const testSchema = Schema({
title: String,
questions: [],
user: Schema.Types.ObjectId
});
testSchema.methods.findAll = async function (){
let tests = await this.find({});
return tests;
}
module.exports = mongoose.model('Test',testSchema)
我正在嘗試在此功能上使用它
showMainPage = function(req, res){
var tests = Test.findAll();
console.log(tests);
res.render('main/index.twig', {username:req.user.username});
}
但我收到此錯誤消息
TypeError: Test.findAll is not a function
如何訪問該功能?
您將findAll
添加為實例方法而不是靜態方法。 如果您希望為模型添加靜態方法,則可以執行以下操作:
testSchema.statics.findAll = async function (){
let tests = await this.find({});
return tests;
}
見靜力學
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.