所以我開發了一個應用程式,我的后端使用 nodejs,在這個應用程式中我需要創建一個調度,所以我需要用戶傳遞他作業的星期幾,我該怎么做?這是我現在的用戶模型代碼:
const mongoose = require('../../database')
const bcrypt = require('bcryptjs')
const nodegeocoder = require('node-geocoder')
const { json } = require('body-parser')
const options ={
provider: 'openstreetmap',
//apiKey: 'WH3W5ZphcP0DSqvD22vgR6N_b6FiWnLWHt9mlSGs9NU',
}
const geocoder = nodegeocoder(options)
const UserScheme = new mongoose.Schema({
title:{
type: String,
required: true,
},
description:{
type: String,
required: false
},
email:{
type: String,
unique :true,
required: true,
lowercase: true
},
contato:{
type: String,
required:false,
select:true
},
horarios:{
type: Date,
required:false,
default:Date.arguments , //not sure about this "horarios"
select: true
},
那么我應該在 horarios 中使用什么?讓它創建一個作業日回圈,這樣他就可以說出他在哪個作業日作業
})
UserScheme.pre('save', async function(next){
const hash = await bcrypt.hash(this.password, 10)
this.password = hash
next();
})
const user = mongoose.model('User', UserScheme)
module.exports = user
uj5u.com熱心網友回復:
不完全確定你的問題是什么,但我想你的意思是如何指定他將在哪幾天作業?在這種情況下,您可以創建一個陣列,如下所示,
"horarios":[
"monday":{startTime: something, endTime: something},
"tuesday":{startTime: something, endTime: something},
...
...
"saturday":{startTime: something, endTime: something},
]
而不是“星期一”,“星期二”..“星期日”,你可以有像 0,1,2..6 這樣的數字,如果這更適合編程目的。
uj5u.com熱心網友回復:
理想情況下,管理此問題的可擴展解決方案是使用cron jobs.
這是一個人可能會如何去做。
- 創建一個模型來存盤作業資料。
- 從您的前端,用戶可以選擇每周/每月等,將詳細資訊保存在作業資料模型中。
- 每 5 分鐘運行一次 cron 作業以檢查需要運行的作業。為此,從需要在接下來的 5 分鐘內運行的作業模型中獲取資料。
- 更新作業模型中的作業狀態。
對于模型,可以使用以下內容:-
'use strict';
module.exports = (sequelize, DataTypes) => {
const ScheduledEvent = sequelize.define('scheduled_events', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false
},
event_id: {
type: DataTypes.STRING,
allowNull: false,
},
event: {
type: DataTypes.JSONB,
allowNull: false,
},
job_id: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
underscored: true,
});
ScheduledEvent.associate = function(models) {
// associations can be defined here
ScheduledEvent.belongsTo(models.User);
};
return ScheduledEvent;
};
還有其他方法可以解決這個問題,但從可擴展性的角度來看,這種方法效果很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323866.html
