Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 续集1:N未找到关联_Javascript_Node.js_Postgresql_Sequelize.js_Eager Loading - Fatal编程技术网

Javascript 续集1:N未找到关联

Javascript 续集1:N未找到关联,javascript,node.js,postgresql,sequelize.js,eager-loading,Javascript,Node.js,Postgresql,Sequelize.js,Eager Loading,我有两个模型:User和Foto,其中每个用户都可以拥有NFoto,并且每个Foto都与1用户关联 因此,我需要的是急切地加载Foto(我传递一个特定的id来检索Foto)和与该Foto关联的用户 我的用户型号 "use strict"; var sequelize = require('./index'); var bcrypt = require('bcrypt-nodejs'); var Foto = require('./Foto'); module.exports = functio

我有两个模型:UserFoto,其中每个用户都可以拥有NFoto,并且每个Foto都与1用户关联

因此,我需要的是急切地加载Foto(我传递一个特定的id来检索Foto)和与该Foto关联的用户

我的用户型号

"use strict";
var sequelize = require('./index');
var bcrypt = require('bcrypt-nodejs');
var Foto = require('./Foto');

module.exports = function (sequelize, DataTypes) {
  var User = sequelize.define("User", {
    username: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        isUnique: function (value, next) {
          var self = this;
          User.find({ where: { username: value } })
            .then(function (user) {
              // reject if a different user wants to use the same username
              if (user && self.id !== user.id) {
                return next('username already in use!');
              }
              return next();
            })
            .catch(function (err) {
              return next(err);
            });
        }
      }
    },

    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        isUnique: function (value, next) {
          var self = this;
          User.find({ where: { email: value } })
            .then(function (user) {
              // reject if a different user wants to use the same email
              if (user && self.id !== user.id) {
                return next('Email already in use!');
              }
              return next();
            })
            .catch(function (err) {
              return next(err);
            });
        }
      }
    },

    typeOfUser: {
      type: DataTypes.INTEGER,
      allowNull:true,
      defaultValue:null
    },

    country: {
      type: DataTypes.STRING,
      allowNull:true,
      defaultValue:null
    },

    birthDate:{
      type: DataTypes.DATEONLY,
      allowNull:true,
      defaultValue:null
    },

    reports: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },

    points: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },

    password: {
      type: DataTypes.STRING,
      allowNull:false
    },

    numberFotos: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    }
  }, {
      classMethods: {
        generateHash: function (password) {
          return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
        },
        associate: function(models){
          User.hasMany(models.Foto,{foreignKey: "userId"});
        }

      },
      instanceMethods: {
        validPassword: function (password) {
          return bcrypt.compareSync(password, this.password);
        }
      }

    });

  return User;
}
重要部分:

   associate: function(models){
      User.hasMany(models.Foto,{foreignKey: "userId"});
    }
我的Foto型号:

"use strict";
var sequelize = require('./index');
var bcrypt = require('bcrypt-nodejs');
var User = require('./User');


module.exports = function (sequelize, DataTypes) {
  var Foto = sequelize.define("Foto", {
    reports: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    image: {
      type: DataTypes.STRING,
      allowNull: false
    },
    date: {
      type: DataTypes.DATE,
      allowNull: true
    },
    lat: {
      type: DataTypes.STRING,
      allowNull: true
    },
    lon: {
      type: DataTypes.STRING,
      allowNull: true
    },
    altitude: {
      type: DataTypes.STRING,
      allowNull: true
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    plantId: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
  },
    {
      associate: function (models) {
        Foto.belongsTo(models.User);
      }
    }
  );


  return Foto;
}
{
      associate: function (models) {
        Foto.belongsTo(models.User);
      }
    }
  allPictures: function (req, res) {
        Foto.findAll({include: [{model: User, as: 'User'}]})
        .then(function (fotos) {
            res.send(fotos);
        })
    }
重要部分:

"use strict";
var sequelize = require('./index');
var bcrypt = require('bcrypt-nodejs');
var User = require('./User');


module.exports = function (sequelize, DataTypes) {
  var Foto = sequelize.define("Foto", {
    reports: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    image: {
      type: DataTypes.STRING,
      allowNull: false
    },
    date: {
      type: DataTypes.DATE,
      allowNull: true
    },
    lat: {
      type: DataTypes.STRING,
      allowNull: true
    },
    lon: {
      type: DataTypes.STRING,
      allowNull: true
    },
    altitude: {
      type: DataTypes.STRING,
      allowNull: true
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    plantId: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
  },
    {
      associate: function (models) {
        Foto.belongsTo(models.User);
      }
    }
  );


  return Foto;
}
{
      associate: function (models) {
        Foto.belongsTo(models.User);
      }
    }
  allPictures: function (req, res) {
        Foto.findAll({include: [{model: User, as: 'User'}]})
        .then(function (fotos) {
            res.send(fotos);
        })
    }
在我的控制器中,我试着像这样快速加载:

"use strict";
var sequelize = require('./index');
var bcrypt = require('bcrypt-nodejs');
var User = require('./User');


module.exports = function (sequelize, DataTypes) {
  var Foto = sequelize.define("Foto", {
    reports: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    image: {
      type: DataTypes.STRING,
      allowNull: false
    },
    date: {
      type: DataTypes.DATE,
      allowNull: true
    },
    lat: {
      type: DataTypes.STRING,
      allowNull: true
    },
    lon: {
      type: DataTypes.STRING,
      allowNull: true
    },
    altitude: {
      type: DataTypes.STRING,
      allowNull: true
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    plantId: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
  },
    {
      associate: function (models) {
        Foto.belongsTo(models.User);
      }
    }
  );


  return Foto;
}
{
      associate: function (models) {
        Foto.belongsTo(models.User);
      }
    }
  allPictures: function (req, res) {
        Foto.findAll({include: [{model: User, as: 'User'}]})
        .then(function (fotos) {
            res.send(fotos);
        })
    }

如果将
定义为相关的
属性,则还应在include中将
作为
参数传递。在本例中,您将
belongsTo
定义为
参数,而不将
定义为
参数。所以查询应该是
Foto.findAll({include:[{model:User}]})
已经尝试过了,但仍然不起作用:/try在关联和查询中添加
as
prop