Reputation: 1531
I have one mongoose model where I declared 1 statics which should return me all features.
const Feature = require('./featureModel')
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schemaOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
};
const ModemSchema = new Schema({
proxy_type: {
type: String,
default: 'None'
},
data_prioritization: {
type: String,
default: 'None'
}
}, schemaOptions);
ModemSchema.statics.possibleProxyTypes = async function () {
features = await Feature.find({})
return features
}
module.exports = mongoose.model('Modem', ModemSchema);
Modem.possibleProxyTypes
I am calling it like this (class method)
But await is not waiting and I am getting output [AsyncFunction]
. Not sure what is wrong here.
Upvotes: 1
Views: 1193
Reputation: 17868
I made it worked like this. ( If you had added all the related codes to the question, I would say where was the problem.)
Modem Schema: ( almost no change, I only added let features, and console.log)
ModemSchema.statics.possibleProxyTypes = async function() {
letfeatures = await Feature.find({});
console.log(features);
return features;
};
And I tried this in a sample get route like this:
const Feature = require("../models/featureModel");
const Modem = require("../models/modemModel");
router.get("/modem", async (req, res) => {
const features = await Modem.possibleProxyTypes();
res.send(features);
});
Maybe the problem was that you didn't use await here in this line:
await Modem.possibleProxyTypes()
This returned me features like this:
[
{
"_id": "5e0207ff4323c7545026b82a",
"name": "Feature 1",
"__v": 0
},
{
"_id": "5e0208054323c7545026b82b",
"name": "Feature 2",
"__v": 0
}
]
Upvotes: 2