简体   繁体   中英

#Sequelize while adding retrieve all attributes

I would like to find a way to retrieve all my attributes while inserting in my Database.

models.association.build(associationParams)
    .save()
    .then(function(assoAdded){
        return next(assoAdded);
    }).catch(function(err){
        // # TODO : implement error Handler
        return next(err);
    });

I got this :

{
   "idAssoParente": null,
   "id": 420,
   "name": "a",
   "email": "aa@aa.aa",       
   "updated_at": "2015-07-29T17:12:47.000Z",
   "created_at": "2015-07-29T17:12:47.000Z"
}

But I want to return all my fields like description , phone , city from my database even if they are empty. Should I necessarily do a find after adding to get all my fields or does it exist a way to retrieve my fields without doing an other request ? Thanks

In short, yes you would need to query your db to return the info. I just started using Sequelize but I found the following worked for me.

// if all the info you need is in your user  
Users.build({req.body})
        .save()
            .then(function(newUser){
               Users.find({where: {UserID: newUser.UserID}}).then(function(user){
                 //resolve your promise as you please.
               });

  // or if address info is in another model you can use eager loading.
  Users.build({req.body})
        .save()
            .then(function(newUser){
               Users.find({where: {UserID: newUser.UserID},
                                  include: [{
                                      model: address
                                  }]
                          }).then(function(user){
                 //resolve your promise as you please.
               });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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