2015-09-21 61 views
2

如何設置sequelize現有帆項目

// finding users 
 
    find: function (req, res, next) { 
 
     var userId = req.body.userId; 
 
     var userSchema = db.UserModel; 
 
     try { 
 

 
     db.sequelize.sync().then(function (params) { 
 
      userSchema.findAll({where:{userId:userId}}).then(function (user) { 
 

 
      return res.json({ 
 
       success:true, 
 
       userData:user.get({ 
 
       plain:true 
 
       }) 
 
      }); 
 

 
      }); 
 

 
     }); 
 

 
     } catch (ex) { 
 
     res.json({ 
 
      success: false, 
 
      exception: ex 
 
     }); 
 
     return; 
 
     } 
 
    }

我書面方式簡單的CRUD API爲我的項目。 而上面提到的執行代碼,我收到錯誤我的控制檯上爲:

Executing (default): SELECT userId , firstname , lastname , email , createdAt , updatedAt FROM Users AS User WHERE User . userId = '1'; Unhandled rejection TypeError: Cannot read property 'get' of undefined at D:\tecsol\mtv\api\controllers\UserController.js:52:27 at processImmediate [as _immediateCallback] (timers.js:367:17)

回答

1

user是一個數組 - 使用findOneuser[0]