`Cant return Array of Object Instead Returns a Single Object

//find all wallet history - Model Function

func FindAllHistory(condition interface{}) (WalletTransactionModel, error) {

db := common.GetDB()

var model WalletTransactionModel

err := db.Where(condition).Find(&model).Error

return model, err

}

2 Likes

Welcome to Go Forum :rocket::fireworks:.


And your question is?

Please note that:

  1. we’re unsure WalletTransactionModel without its struct descriptions.
  2. common is which database package?
1 Like

This is a Function in my Model file that is suppose to collect all records based on condition, so example is that I queries and it returns a single object, but My terminal indicates atleast 5 rows where affected. So my challenge is that I seem not to Know how to send array of obects to the calling function for display. So the problem is that it fetches more than one record but return only one

2 Likes

type WalletTransactionModel struct {
ID int gorm:"primary_key;AUTO_INCREMENT"
Category string gorm:"not null"
Sender string gorm:"not null"
Receiver string gorm:"not null"
Description string gorm:"not null"
SessionID string gorm:"-"
PIN string gorm:"-"
OriginatingTransaction int
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}

This is my

2 Likes

We need to know more about FindAllHistory(...), does it having a documentations (those comments on top of this functions?

A word of warning: since this is related to financial data (either real money or concurrency), please be careful and take your time filtering out sensitive information.

1 Like

You should pass an array pointer to the Find method, not a single object.

var model WalletTransactionModel
datas := []WalletTransactionModel{}

err := db.Where(condition).Find(&datas).Error
2 Likes

Thanks @kync. This Executes But I still get One instance of my the Object instead of two instance returned as indicated in my terminal

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.