1 回答
TA贡献1966条经验 获得超4个赞
我不认为模型/模式可以是异步的,但由于您需要一个异步默认值,您可以试试这个:
const pricesSchema = mongoose.Schema({
USD_LOWEST: {
type: Number,
required: true,
},
USD_LOW: {
type: Number,
required: true,
},
USD_HIGH: {
type: Number,
required: true,
},
USD_HIGHEST: {
type: Number,
required: true,
},
USD_CBA: {
type: Number,
required: true,
},
BTC_PRICE: {
type: Number,
required: true,
},
date: {
type: Date,
expires: 60 * 60 * 24 * 7,
},
});
pricesSchema.pre('save', async function () {
if (!this.date) {
const response = await axios.get('http://worldtimeapi.org/api/timezone/Asia/Yerevan');
this.date = response.data.datetime;
}
});
export const Price = mongoose.model('Prices', pricesSchema);
添加回答
举报