在 Node JS 应用程序中部署后,根据存储时间对数据进行排序不起作用

发布于 2025-01-17 07:58:47 字数 2767 浏览 0 评论 0原文

我开发了一个简单的 MERN 堆栈应用程序,为此,我想根据用户(在我的后端)获取的时间对数据进行排序,因此在我的 node.js 后端编写了以下代码it:-

当程序执行达到 check1 和 check2 都存在的条件时,当我在本地主机(本地)上运行应用程序时,它可以正常工作并提供所需的输出,但是当我在heroku上部署应用程序时,该块代码没有给出同一组数据的相同/所需输出。

使用完全相同的代码,为什么会出现这种差异,我无法找出解决此问题的任何解决方案/替代方法会有所帮助。

const allDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

const getCurrentDay = () => {
  let date = new Date();
  let daynum = date.getDay();
  return allDays[daynum];
}

const getCurrentHour = () => {
  return parseInt(new Date().getHours())
}

const sortSubject = (a, b) => {
  let day = getCurrentDay();

  let check1 = a.routine.find((obj) => {
    return obj.day === day;
  })

  let check2 = b.routine.find((obj) => {
    return obj.day === day;
  })

  if (check1 && check2) {
    const hours1 = check1.time.split(":")[0];
    const hours1 = check2.time.split(":")[0];

    const hrs1 = parseInt(hours1);
    const hrs2 = parseInt(hours2);
    const currHour = getCurrentHour();

    if (Math.abs(hrs1 - currHour) > Math.abs(hrs2 - currHour)) {
      return 1;
    }

    return -1;
  } else if (check1) {
    return -1;
  }

  return 1;
}

这是供参考的屏幕截图:-

在本地主机 在本地主机上

部署版本 部署版本

编辑 这是我的主题模型的架构:-

const subjectSchema = new mongoose.Schema({
    user:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"user",
        required:true
    },
    subName:{
        type:String,
        required:true
    },
    routine:{
        type: [{day:String,time:String,link:String}]
    }
});

这是我在将用户的所有主题发送到客户端之前获取用户的所有主题时调用特定函数的地方,我对其进行排序

router.get(
    "/getsubjects",
    authorizeuser,
    async(req,res)=>{
        try{
            //Find all the subjects of the logged in user
            const subjects = await Subject.find({user:req.user.id});
            subjects.sort(sortSubject);
            return res.json({subjects});
        }catch(err){
            console.log(err);
            res.status(500).json({error:"Some Internal Error Occured"});
        }
    }
)

这是一个示例主题:- 输入图片此处描述

I developed a simple MERN stack application and for that one of the use case was that I wanted to sort my data based upon the time that I took from user (in my backend) and thus wrote the following code in my node.js backend for it:-

When the program execution comes to the condition where check1 and check2 both are present then when I am running the application on localhost (locally) then it is working correctly and giving desired output but when I deployed the application on heroku then that block of code is not giving the same/desired output for the same set of data.

With the exact same code why is this difference I am unable to figure out any solution/alternative way to this problem would be helpful.

const allDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

const getCurrentDay = () => {
  let date = new Date();
  let daynum = date.getDay();
  return allDays[daynum];
}

const getCurrentHour = () => {
  return parseInt(new Date().getHours())
}

const sortSubject = (a, b) => {
  let day = getCurrentDay();

  let check1 = a.routine.find((obj) => {
    return obj.day === day;
  })

  let check2 = b.routine.find((obj) => {
    return obj.day === day;
  })

  if (check1 && check2) {
    const hours1 = check1.time.split(":")[0];
    const hours1 = check2.time.split(":")[0];

    const hrs1 = parseInt(hours1);
    const hrs2 = parseInt(hours2);
    const currHour = getCurrentHour();

    if (Math.abs(hrs1 - currHour) > Math.abs(hrs2 - currHour)) {
      return 1;
    }

    return -1;
  } else if (check1) {
    return -1;
  }

  return 1;
}

Here is the screenshot for reference:-

On localhost
On localhost

Deployed Version
Deployed Version

Edit
Here is the schema for my subject model:-

const subjectSchema = new mongoose.Schema({
    user:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"user",
        required:true
    },
    subName:{
        type:String,
        required:true
    },
    routine:{
        type: [{day:String,time:String,link:String}]
    }
});

Here is where I am calling the particular function when I fetched all the subjects of a user before sending it to the client I sort it

router.get(
    "/getsubjects",
    authorizeuser,
    async(req,res)=>{
        try{
            //Find all the subjects of the logged in user
            const subjects = await Subject.find({user:req.user.id});
            subjects.sort(sortSubject);
            return res.json({subjects});
        }catch(err){
            console.log(err);
            res.status(500).json({error:"Some Internal Error Occured"});
        }
    }
)

Here is an sample subject:-
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文