-1
Good evening. I just started studying Node.js and am having trouble using . create in Sequelize.
The error happens when I try to register a user:
routes.get('/', async (req, res) => {
const user = await User.create({
'name': 'nome',
'email': '[email protected]',
'password_hash': '1234567890'
});
return res.json('user');
});
The error returned is:
(node:13148) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
at User._initValues
(node:13148) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:13148) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Ps: it’s the first time messing with Sequelize, I tried to use Try/catch but it stayed the same.
Hello Shedyhs, give a console.log in user and post the return here. Thus: Return console.log(user), the object is Undefined and therefore does not have the length property
– Lucas Travagin
Opa Lucas, did not return something different. (Node:9620) Unhandledpromiserejectionwarning: Typeerror: Cannot read Property 'length' of Undefined (Node:9620) Unhandledpromiseionwarning: Unhandled Promise rejection. This error either originated by Throwing Inside of an async Function without a catch block, or by rejecting a Promise which was not handled with . (catch). (rejection id: 1) (Node:9620) [DEP0018] Deprecationwarning: Unhandled Promise rejections are deprecated. In the Future, Promise rejections that are not handled will terminate the Node.js process with a non-zero Exit code.
– Shedyhs
Comments on "res.json('user')" and a console.log in this user variable. I’m trying to understand, if this User.create is instantiated, because this User.create is a method of your model, right?
– Lucas Travagin
User is a Schema or
.create
is a Sequelize function where I pass the object to it to add the users table. when I from the console.log it does not return anything to me. I guess it’s because the user only gets value after the async is over.– Shedyhs
You are returning a string "user", not the created object
– Rafael Tavares