简体   繁体   English

从 Sequelize 返回值

[英]Return value from Sequelize

The below code is to get due amount by subtracting actual fee with amount paid.下面的代码是通过用支付的金额减去实际费用来获得到期金额。 I get value returned as null instead of difference.我得到的值返回为 null 而不是差异。 WHen i add return for db.XXX.findOne, I get error as "Invalid value Promise".当我为 db.XXX.findOne 添加 return 时,我收到错误消息“无效的值承诺”。 Please help me understand where i went wrong请帮助我理解我哪里出错了

let x=getDueAmount(1,200);

const getDueAmount = (id,amountpaid) => {
  let due;
  db.XXX.findOne({
    where:{id: id},
    attributes:['fees']
   }).then(feeDetail=>{
      due=feeDetail.fees-amountpaid;
       });
   return due;
}

Simply return from your promise (your Db call).只需从您的承诺(您的 Db 调用)返回。 In your code your returning the value of due prior to the result coming back from the db.在您的代码中,您在从数据库返回结果之前返回due的值。 Read up on promises and asynchronous javascript阅读 Promise 和异步 javascript

let x=getDueAmount(1,200);

const getDueAmount = (id,amountpaid) => {
  return db.XXX.findOne({
    where:{id: id},
    attributes:['fees']
   }).then(feeDetail=>{
     return feeDetail.fees-amountpaid;
   });
}

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

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