• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How to Fix SQL Scan Error in Go for User Login?

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Introduction


If you're developing a user login feature in Go and encounter the error sql: expected 15 destination arguments in Scan, not 18, you're not alone. This common issue typically arises when there is a mismatch between the number of columns returned by your SQL query and the number of variables to which you are scanning the results. In this article, we'll explore how to fix this error and make sure your user authentication works seamlessly based on an email and password.

Understanding the Error


The error message ‘sql: expected 15 destination arguments in Scan, not 18’ indicates that your SQL query is returning more columns than you are attempting to scan into your variables. In your case, the SQL query is trying to fetch 15 columns from the users table, while the Scan method is set up to receive 18 values. Here's how we can resolve this issue.

Examination of the SQL Query


Let’s first look at the SQL query in your FindByEmail function:

query := `
SELECT
id, email, password, role, name, date_of_birth, profile_photo,
phone_number, gender, address, blood_type, rhesus, google_id,
fcm_token, created_at, updated_at
FROM users
WHERE email = $1
LIMIT 1
`


This SQL query selects 15 columns from the users table.

The Scan Function


Your Scan function currently attempts to scan 18 values:

err := r.db.QueryRowContext(ctx, query, email).Scan(
&user.ID,
&user.Email,
&user.Password,
&user.Role,
&user.Name,
&user.DateOfBirth,
&profilePhoto,
&user.PhoneNumber,
&user.Gender,
&user.Address,
&user.BloodType,
&user.Rhesus,
&googleID,
&fcmToken,
&user.CreatedAt,
&user.UpdatedAt,
)


From this code, we can see that the additional fields you are trying to scan:

  • user.TotalDonation
  • user.Coin
  • profilePhoto, googleID, fcmToken remain NULL or not set.
Adjusting the Scan Function


To resolve this, you must adjust your scan call to match the columns selected in your query.

  1. Remove the extra fields from the Scan method.
  2. Ensure that only the variables necessary for the returned columns are provided.

Here’s the revised Scan method:

err := r.db.QueryRowContext(ctx, query, email).Scan(
&user.ID,
&user.Email,
&user.Password,
&user.Role,
&user.Name,
&user.DateOfBirth,
&profilePhoto,
&user.PhoneNumber,
&user.Gender,
&user.Address,
&user.BloodType,
&user.Rhesus,
&googleID,
&fcmToken,
&user.CreatedAt,
&user.UpdatedAt,
)


In this case, you won't scan TotalDonation and Coin as these should be set to their default values when creating a user.

Setting Default Values


You can set defaults for those fields after scanning:

user.TotalDonation = 0
user.Coin = 0

Final Implementation


Make sure your FindByEmail function looks like this:

er := r.db.QueryRowContext(ctx, query, email).Scan(
&user.ID,
&user.Email,
&user.Password,
&user.Role,
&user.Name,
&user.DateOfBirth,
&profilePhoto,
&user.PhoneNumber,
&user.Gender,
&user.Address,
&user.BloodType,
&user.Rhesus,
googleID,
fcmToken,
&user.CreatedAt,
&user.UpdatedAt,
)

Frequently Asked Questions


Q: Why am I still getting errors after changing the code?
A: Make sure you review both your SQL query and your scan parameters for consistency. Count the number of selected fields and make sure they align with your scan destinations.

Q: Can I use nullable fields and still avoid this error?
A: Yes, your nullable fields can still be scanned, but ensure they match the expected column count in your query.

Q: How should I handle errors during scanning?
A: Always check if err != nil after calling Scan and handle known errors like sql.ErrNoRows accordingly.

Conclusion


By making a few adjustments to your scan function and ensuring the fields align with your SQL query, you can effectively resolve the error and create a smooth login experience for users. Now, you should be able to authenticate users based on their email and password without encountering scanning issues in your Go application.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу