将3个表与一个表连接起来

时间:2019-06-24 09:45:26

标签: javascript mysql arrays node.js join

我需要将3个表与一个主表连接起来。

主表-监控器, 辅助表格-RafflePoint,RaffleCode,RaffleWinner

在下面附加图片。

Monitor Table

RafflePoint

RaffleCode RaffleWinner

我的代码看起来像这样。

raffleStatus.js

MonitorModel.findOne({
  where: {
    id: monitor.id
  },
  include: [
      {
        model: MonitorRafflePoints,
        where:{
          monitor_id: monitor.id, 
          month: currentUTCTime
        }
      }
      ,{
        model: MonitorRaffleCode,
        where: {
          monitor_id : monitor.id, 
          created_at : { $between: [ startOfMonth, endOfMonth ] }
        }
      }
      ,{
        model: RaffleWinnersModel,
        where: {
          monitor_id: monitor.id,
          date : { $between: [ startOfMonth, endOfMonth ] }
        }
      }
  ]
})
.then(function(monitor){

    // monitor = JSON.stringify(monitor);
    console.log('After stringify ----> ', typeof (monitor)); 

    if(monitor){
      console.log(' <---- Inside If ----> ', monitor)
      monitor.MonitorRafflePoints.forEach(function(points){
        monitorPoint = points;
      });
      console.log('Monitor Raffle Points ----> ',monitorPoint);

       monitor.MonitorRaffleCodes.forEach(function(codes){
         monitorCodes = codes;
       });

       monitor.MonitorWinner.forEach(function(winners){
         monitorWinner = winners;
       });
      return getLastWinnerDetails();
    }else{
      console.log(' <---- Inside Else ---->', monitor);

      monitorPoint = JSON.stringify(monitor.MonitorRafflePoints);
      monitorCodes = JSON.stringify(monitor.MonitorRaffleCodes);
      monitorWinner = JSON.stringify(monitor.RaffleWinners);
      return getLastWinnerDetails();
    }
}).then(function(winner){
     var response  = {};
       console.log('Last Response ----->',response);
     return res.json(response);
})

此表的关联如下。

index.js

 // Raffle relations with Monitor
db.Monitor.hasMany(db.MonitorRafflePoints, {foreignKey: 
'monitor_id'});
// db.Monitor.belongsTo(db.MonitorRafflePoints, {foreignKey: 
'monitor_id'});
db.MonitorRafflePoints.belongsTo(db.Monitor, {foreignKey: 
'monitor_id'});
db.Monitor.hasMany(db.MonitorRaffleCode, {foreignKey: 
'monitor_id'});
db.MonitorRaffleCode.belongsTo(db.Monitor, {foreignKey: 
'monitor_id'});
db.Monitor.hasMany(db.RaffleWinners, {foreignKey: 'monitor_id'});
db.RaffleWinners.belongsTo(db.Monitor, {foreignKey: 
'monitor_id'});
db.MonitorRafflePoints.hasMany(db.MonitorRaffleCode, {foreignKey: 
'monitor_id'});
// db.MonitorRaffleCode.belongsTo(db.MonitorRafflePoints, 
{foreignKey: 'monitor_id'});

每当我运行这段代码时,我似乎都会得到错误而不是得到结果。

Error Screen

我在做错什么,将不胜感激。

重新编辑- 从Monitor表成功获取Monitor_id后,应在RafflePoint表中查找具有相同Monitor_id的MonitorRafflePoint,然后在找到时,下一步应在RaffleCode表中查找具有相同monitor_id的RaffleCode。 接下来,应该从RaffleWinner表中获取rafflewinner。

最后为了获得LastWinnerDetails,我编写了一个小函数。

raffleStatus.js

getLastWinnerDetails函数

var getLastWinnerDetails = function(){
  return RaffleWinnersModel.find({
    include : [{
      model : MonitorModel,
      include : [{
        model : PatientModel,
     }]
  }],
  limit: 1,
   order: [ [ 'date', 'DESC' ]]
   });
}

1 个答案:

答案 0 :(得分:0)

仅当monitor为null时,您的代码才会达到else条件(Inside Else)。您在monitorPoint = JSON.stringify(monitor.MonitorRafflePoints);

遇到此错误

因此,您无法访问monitor.MonitorRafflePoints,因为monitor为空。