Reputation: 41
I wanted to created a transaction module where after a successful transaction the users document(in this case user sends money to another user) will be updated as well.
a. in user.js(this is user model) besides name, pw, email (etc) I created this property which will hold the transaction related history of respective user. Please look at how I’ve used it:
transaction_history:[{
transactionid:String,
timestamp:String,
type:String,
balance:Number,
status:String
}]
b. when sender clicks send button in the form, a transaction document is created, & after this the user document(here the sender) should be updated along with transaction info.
//create transaction document, works fine
Transaction.create({transactionid:uniqid(),
timestamp:moment().format(datemask),
amount:balance,
sender:sendfrom,
receiver:sendto,
status:"done"
}, function(err, tr){
if(err) throw err;
else {
//I want sender document to update with transaction info
User.findOne({email:sendfrom}, function(err, sendfrom){
if(err) {console.log("error at sender side");}
else
if(sendfrom!=null){
// console.log("tr: "+tr); //fine
sendfrom.balance-=balance;
sendfrom.transaction_history.push({
transactionid:tr.transactionid,
// timestamp:tr.timestamp,
// type:"debit",
// balance:tr.amount,
// status:tr.status
}
);
sendfrom.save();
console.log("sender's current balance is: "+sendfrom.balance);
};
});
}});
c. But then I get this:
events.js:163
throw er; // Unhandled 'error' event
^
CastError: Cast to string failed for value "{ transactionid: '1amhrummxjhnhv0w4' }" at path "transaction_history"
Why this error occurs?I want your suggestion please! Thank you
Upvotes: 1
Views: 10331
Reputation: 33
There is a workaround provided by Mongoose:
You can use the field "type" in the object as long as you define its type, as such:
transaction_history: [
{
transactionId: String,
timestamp: String,
type: { type: String },
balance: Number,
status: String
}
]
source: https://www.typeerror.org/docs/mongoose/schematypes
Upvotes: 1
Reputation: 602
At my case:
CastError: Cast to String failed for value "[ 'whateverValue_1', 'whateverValue_2' ]" at path "name"
Issue was that I have in form html name same word...
I.e:
<input name = "name"> Name
<input city = "name"> City
Looking for my mistake, I leave you other that could help for newbies as me:
words like type
, model
, never have to be in model Schema as main keywords!
Upvotes: 2
Reputation: 101
You can't use the word 'type' as an object. Just rename to something else like 'something_type'.
Upvotes: 7
Reputation: 41
Root of the problem is in the way I defined transaction_history property. This was supposed to be an array of objects syntactically[{}], but then I wrote its type to be String. So when I tried to insert a string as a value of ’type’, it throws error being unable to push string to ‘transaction_history’ object. To solve it, just need to remove type property. It’s my mistake to use a reserved word as a key in object.So I replaced 'type' by something else in model. that’s it!
Upvotes: 2