3

This is my function:

/* function for add transaction */
function addtransaction(user_id,status,amount,date,description){
  var saved = "";
  function getLastId(){
    Transactions.findOne({}, {}, { sort: { '_id' : -1 } }).lean().exec(function(err, lastTrans) {
        if (err) {
          return 1;
        }else{
          return parseInt(lastTrans.id) + 1;
        }
    });  
  }

  var newId = getLastId();
  var newTransactions = new Transactions({
      id           : newId,
      user_id      : user_id ,
      status       : status ,
      amount       : amount ,
      date         : date ,
      description  : description
  });

  if (newTransactions.save()){
    return true;
  }else{
    return false;
  }     
}/* end function */

And this is my console error:

Can you help me to solve this error? enter image description here

I updated my function but there is still error!

asp Tec
  • 39
  • 3

3 Answers3

0

It seems like the error is here getLastId + 1 .getLastId is a function and 1 a number, and a function cannot be cast to a number. Most likely you forgot to put the parentheses to get the result out off the function call.

Try replace getLastId + 1 with getLastId() + 1

Walle Cyril
  • 3,087
  • 4
  • 23
  • 55
0

You may missed the () in:

id: getLastId + 1
Xinzoop
  • 111
  • 4
0

Your issue is that you're not waiting for your getLastId() to actually return since it is async. Since it is async, while getLastId() is still working, your code has already moved on to creating a new Transaction where newId is undefined since nothing has been returned. So you need to place your code dependent on newId into the callback of your Transaction.findOne() or pass in a callback to getLastId().

Just to note, your addtransaction function should take a callback function as well so you can return the outcome of newTransactions.save() to the called in an asynchronous way. I did not do this in the below code examples since I do not know how your other code is structured.

I would also say that Approach #1 below is the preferable one since its less code, doesn't involve defining getLastId() everytime addtransaction() is called and it is has less callbacks than Approach #2.

Approach #1, place logic in Transactions.findOne()

function addtransaction(user_id, status, amount, date, description) {
  Transactions.findOne({}, {}, { sort: { '_id' : -1 } }).lean().exec(function(err, lastTrans) {
    var newId = err ? 1 : (parseInt(lastTrans.id) + 1);
    var newTransactions = new Transactions({
      id           : newId,
      user_id      : user_id ,
      status       : status ,
      amount       : amount ,
      date         : date ,
      description  : description
    });

    return newTransactions.save();
  }); 
}

Approach #2, Pass in Callback

/* function for add transaction */
function addtransaction(user_id,status,amount,date,description){
  var saved = "";
  function getLastId(callback){
    Transactions.findOne({}, {}, { sort: { '_id' : -1 } }).lean().exec(function(err, lastTrans) {
        if (err) {
          return callback(null, 1);
        } else {
          return callback(null, parseInt(lastTrans.id) + 1);
        }
    });  
  }

  getLastId((err, newId) => {
    var newTransactions = new Transactions({
        id           : newId,
        user_id      : user_id ,
        status       : status ,
        amount       : amount ,
        date         : date ,
        description  : description
    });

    return newTransactions.save();
  });

}/* end function */

Its also worth noting that assigning return data directly from a function that is async isn't the proper way to construct asynchronous functions in Node.js. See this answer for the proper way to construct these types of functions.

Community
  • 1
  • 1
peteb
  • 18,552
  • 9
  • 50
  • 62
  • Thanks for the complete answer but there is still a problem. I need a result of addtransaction() as "true" of "false" for rest of my process and in this case, addtransaction() returned nothing! Can I return something by main function? – asp Tec Dec 04 '16 at 07:32
  • Well since you were already reacting the the truthiness of the return value from`newTransactions.save()`, simply returning out `newTransactions.save()` would do the same. – peteb Dec 04 '16 at 07:46
  • could you show me an example? I put "return newTransactions.save();" out of "Transactions.findOne" and it made error! I just want to a "true" of "false" of main function ->"addtransaction" just it, can you show me an example? – asp Tec Dec 04 '16 at 08:01