skip to Main Content

I am using mongodb and I am having challenges saving.

My Schema is

const transaction = new Schema({
    sender: {
        type: ObjectId,
        default: null,
        transaction_type: {
            type: String,
            enum: transaction_types,
            default: null
        }
    },
    recipient: {
        type: ObjectId,
        default: null,
        transaction_type: {
            type: String,
            enum: transaction_types,
            default: null
        }
    },
    coins: {
        type: Number
    },
    fee: {
        type: Number,
        default: 0
    },
}, {
    timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleted_at: 'deleted_at'}
})

In my controller, I am doing this

await WalletTransaction.create({
                sender: user._id,
                recipient: recipient,
                coins: coins
            });

How do I save the transaction type alongside the sender and recipient.

Thanks a lot

2

Answers


  1. Chosen as BEST ANSWER

    I had to change my model to

    const transaction = new Schema({
        sender: {
            user: {
                type: ObjectId,
                default: null,
            },
            transaction_type: {
                type: String,
                enum: transaction_types,
                default: null
            }
        },
        recipient: {
            user: {
                type: ObjectId,
                default: null,
            },
            transaction_type: {
                type: String,
                enum: transaction_types,
                default: null
            }
        },
        coins: {
            type: Number
        },
        fee: {
            type: Number,
            default: 0
        },
    }, {
        timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleted_at: 'deleted_at'}
    })
    

    and on saving, I did this

    await WalletTransaction.create({
        sender: {
            user: user._id,
            transaction_type: "sent"
        },
        recipient: {
            user: recipient,
            transaction_type: "received"
        },
        coins: coins
    });
    

  2. Is WalletTransaction the model of the transaction schema? you may do this to save the document:

    const t = await WalletTransaction.create({
                    sender: user._id,
                    recipient: recipient,
                    coins: coins
                });
    await t.save();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search