繁体   English   中英

在对象属性Sequelize中访问数组

[英]Access an array in object property, Sequelize

我有一个带有条目的表格,您可以将其相乘,因此我们有一些名称相同的输入。 所以我的req.body.input返回一个数组。 这是一个测试请求对象:

{
     "artist": "Picasso",
     "title": "Les demoiselles d'Avignon",
     "yearBegin": "1902",
     "yearEnd":"1936",
     "period":"Début XXème",
     "width":"200",
     "height":"65",
     "technical":"Peinture à l'huile",
     "support":"Bois", 
     "isSigned":"true",
     "status":"En cours d'authentification",
     "tags":"",
     "galery":["Perrotin","Moma",""],
     "exhibitionName":["Lalala","A dog in Berlin",""],
     "exhibitionBegin":["2018-04-01","2018-03-01",""],
     "exhibitionEnds":["2018-04-30","2018-03 28",""],
     "localisation":"Allemagne",
     "description":""
}

我想例如在req.body.exhibitionName上循环,但是当我循环为:

for (var y=0; y < req.body.exhibitionName.length; y++) {
   exhibitionController.create(req,res, idCreation, y)
}

在我的控制器中,我有:

module.exports = {
    create(req, res, id, i) {
      Exhibition.sync({force: true}).then(function () {
        return Exhibition
          .create({
              name: req.body.exhibitionName[i],
              galery: req.body.galery[i],
              dateBegin: req.body.exhibitionBegin[i],
              dateEnd: req.body.exhibitionEnds.[i],
              idExhibitionCreation: id
          })
        })
    }
};

它将我推到数据库中的第一个条目galery = P,然后将galery = E和所有字母都放在数组的第一个值中。...所以我的问题是如何从对象属性内的数组中获取数据并在数组上循环?

您的for循环不会等待承诺的实现,因此您需要使用NPMJS上async库中的each函数。

package: https package: //www.npmjs.com/package/async

documentation: https documentation: //caolan.github.io/async/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM