Hello, getting this now: `Field 'username' doesn't...
# box-products
o
Hello, getting this now:
Field 'username' doesn't have a default value
This is the code I am using.
Copy code
// create Action
	function create( event, rc, prc ){
     ...
		var token = generateRandomToken();

		var now = Now();
		var expiration =DateAdd("d", 1, now);

        var user = getInstance( "User" )
			.firstWhere( "email", rc.email )
			if( !isNull( user ) ) {
				user.setToken( token )
				.setExpiration( expiration )
            	.save();
			}
		    relocate( uri = "/login" );
		
	}
I don't see what that has to do with what I'm trying to do.
b
@Ookma-Kyi please include more information. firstly, you say you're getting an error, but where? What line of code? What is the stack trace?
o
Copy code
Error Details
Error Code	---
Type	database
Message	Field 'username' doesn't have a default value
Detail	---
Extended Info	---
Environment	development
Event	forgotpassword.create
Route	forgotpassword/
Route Name	/forgotpassword
Routed Module	---
Routed Namespace	---
Routed URL	forgotpassword/
Layout	---
Module	---
View	---
b
That is not a stack trace (or even a tag context)
All it shows is some ColdBox details, which without access to your app code, are unfortunately not helpful
o
One sec
Interestenly if I comment out the
.save()
ine like so:
Copy code
var user = getInstance( "User" )
			.firstWhere( "email", rc.email )
			if( !isNull( user ) ) {
				user.setToken( token )
				.setExpiration( expiration )
            	//.save();
			}
it doesn't crash
b
So, based on that tag context, I know what I assume the issue to be.
After studying it, what conclusions have you reached?
o
username needs a default value, but why?
b
You tell me, it's your app and DB, lol
I can't begin to tell you how you'be configured your own code and DB tables šŸ™‚
o
I'm using the defaults
b
The error is coming from the DB engine itself
So it would appear you have that column non-nullable and no default value... as the error says
o
My MySQL server?
b
where else?
This is your setup-- if you're using MySQL then I suppose. Again, why are you asking me what Db you're using?
o
I set the value on new account registration, does it really matter?
b
I don't know what that means
But it really doesn't matter- it sounds like you have all the info you need to proceed
o
I guess the db is right, I should set it for sanity reasons
b
You are trying to save a user with no username but that is a required column to have a value
Go forth and solve šŸ™‚
o
Actually the user table is empty
Testing for different scenarios
b
I would assume it's inserting then
A tool like FusionReactor would be very helpful you right now to see the SQL queries that are running
o
And in this case it's a forgot password form
So the only thing I am setting is token and expiration, not sure why it is inserting especially since there is no suck user
b
That's really irrelevant, it could be the form to store launch codes for a nuclear missile, but it doesn't change what you need to do next
I don't know enough about Quick to know if it returns null or an empty object that has a not-loaded flag, but I'd assume it's the latter
I would recommend • reading the Quck does to see how firstwhere() works • Dump out the user to see what it is!!
This is information you can easily obtain through a little research and debugging
o
Looked at fusion, but I can't afford to pay $20/mo sorry 😢
Adds a basic where clause to the query and returns the first result.
b
That doesn't really specify the behavior when not found
Looking at the code (which took only 30 seconds to pull up.. hint hint šŸ˜‰ ) https://github.com/coldbox-modules/quick/blob/0c4bbcf3882df043f49a77a5fcc39ebe29611365/models/BaseEntity.cfc#L1145
It just calls the
.first()
method internally
And first is documented as so
Executes the configured query and returns the first entity found. If no entity is found, returns
null
.
So it would appear it should return
null
This is where your debugging code comes in. Dump out the user object and abort to see what's in it!
o
Actually I already did that:
b
Good, so is there data in it? (your screenshot doesn't really show)
Don't show it to me, lol. Look at it yourself and take the necessary steps. I'm not here to do your job for you, I'm trying to help you do your job šŸ™‚
o
No no, what I trying to say is there is so much information
b
Then you need to work on figuring out what information to look for, or perhaps what helper methods exist on quick entities to view their data
o
Found it:
The properties are null
b
not sure how you're getting an empty object based on what the code/docs say. Perhaps @elpete can shed light on that
You're dumping out that user object inside the
!isNull( user )
check, right?
o
no outside
b
What, why??
How do you know you're dumping out the result of the firstWhere() call?
If you are just dumping out
user
with no scope, it could be finding another unrelated user object from another scope entirely!
For instance, you may have code elsewhere on the page creating an empty user object in the variables scope that is what's getting used here
Without seeing your entire app it's just impossible to tell what you have going on