ReferenceError: user is not defined in user api - sails.js

When I open localhost:1337/user I got Internal Server Error
Something isn't right here.
ReferenceError: user is not defined
How can I fix it? My API is not working. I can't insert data in database using html forms in sailsjs.
My user model is:
module.exports = {
attribute: {
firstname: {
type: 'string',
unique: true
},
lastname: {
type: 'string',
unique: true
},
organisationName: {
type: 'string',
unique: true
},
DepatmentName: {
type: 'string',
unique: true
},
DOB: {
type: 'date',
unique: true
},
DOJ: {
type: 'date',
},
DOL: {
type: 'date',
},
Address: {
type: 'string',
},
City: {
type: 'string',
},
State: {
type: 'string',
},
Country: {
type: 'string',
},
email: {
type: 'string',
email: true,
required: true,
unique: true
},
encryptedPassword: {
type: 'string',
},
ContactNumber: {
type: 'integer',
},
//toJSON: function() {
// var obj = this.toObject();
//
// }
}
};

You are calling a variable "user" and it doesn't exist or it's empty. Check youre code for this variable..
In youre usermodel, you only have firstname and lastname. I guess you need one of this. If you want more specific help, then you have to show us some extra coding of the page, not only the model.

Related

I have an 'UsageError' on SailsJs with waterline-orm model method '.archive()'

this is error return in postman when i try to archive a record in my Sails backend.
UsageError: Invalid initial data for new records.\nDetails:\n Could not use one of the provided new records: Missing value for required attribute id. Expected a string, but instead, got: undefined\n [?] See https://sailsjs.com/support for help
Please somebody know what this error means? Thanks in advance.
Thanks for your response
My code:
- In controller:
try {
await Laboratory.archive({id: inputs.id});
} catch (error) {
console.log('Error-6 CORE-DELETE_LABORATORY:', error)
exits.failed(`Error-6 CORE-DELETE_LABORATORY: ${error}${inputs.id}`)}
In file config/models.js:
module.exports.models = {
migrate: 'alter',
fetchRecordsOnUpdate: true,
fetchRecordsOnCreate: true,
fetchRecordsOnCreateEach: true,
attributes: {
createdAt: { type: 'ref', columnType: 'datetime', autoCreatedAt: true, },
updatedAt: { type: 'ref', columnType: 'datetime', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
},
dataEncryptionKeys: {
default: 'dRYQHBf8Zpza2vMS5BB3qcSiLspJP4BG37I2JkP2yYw='
},
cascadeOnDestroy: true
};
Laboratory model:
module.exports = {
attributes: {
type: {
type: 'string',
isIn: ['INSIDE', 'OUTSIDE'],
required: true,
},
raisonSocial: {
type: 'string',
required: true,
unique: true,
},
city: {
type: 'string'
},
address: {
type: 'string'
},
email: {
type: 'string'
},
neighborhood: {
type: 'string'
},
contacts: {
type: 'string'
},
logo: {
type: 'string'
},
lat: {
description: 'Latitude',
type: 'number'
},
long: {
description: 'Longitude',
type: 'number'
},
website: {
type: 'string',
defaultsTo: ''
},
subdomain: {
type: 'string',
unique: true,
required: true,
},
mobileMoney: {
type: 'string'
},
bank: {
type: 'string',
defaultsTo: ''
},
bankAccountID: {
type: 'string',
defaultsTo: ''
},
validated : {
type: 'boolean',
defaultsTo : false
},
f_establishment: {
model: 'establishment'
},
director: {
description: 'Chef of laboratory id',
type: 'json',
example: '{name, phone, role}',
required: true,
}
},
customToJSON () {
if (this.logo) {
this.logo = `${process.env.BASE_URL}/avatar/${this.logo}`
}
return this
}
};
Thanks.
Sails throws UsageError when we use a db method in an incorrect way. To be more precise, this is what Sails has in its documentation.
When an error has name: 'UsageError', this indicates that a Waterline
method was used incorrectly, or executed with invalid options (for
example, attempting to create a new record that would violate one of
your model's high-level validation rules.)
https://sailsjs.com/documentation/concepts/models-and-orm/errors#?usage-errors
To give you a simple example, if you have a db query with limit parameter and you start passing string values for that parameter, Sails with start throwing UsageError.
In your case, things should be self explanatory from the error detail itself. Your code flow is getting undefined for inputs.id when it tries to run Laboratory.archive({id: inputs.id}) but id is expected to be a string if you see config/models.js.
Just do a basic debugging to see why inputs.id is coming as undefined and solve that issue, and you should be good to go.

Sails 1.0 - sails-disk for dev/test and sails-mongo for production

In Sails 1.0, I'm trying to use sails-mongo for dev and production and sails-disk for test. Before version 1.0 I could do this, but now I receive an error:
error: Error: In model 'user', primary key 'id' must have either
'required' or 'autoIncrement' set.
My code:
module.exports = {
attributes: {
createdAt: { type: 'number', autoCreatedAt: true },
updatedAt: { type: 'number', autoUpdatedAt: true },
id: { type: 'string', columnName: '_id' }, //<---- error Here!
name: {
type: 'string',
required: true
},
......
}
Can I do some workaround to make it work?
Add a required rule on your id field.
module.exports = {
attributes: {
createdAt: { type: 'number', autoCreatedAt: true },
updatedAt: { type: 'number', autoUpdatedAt: true },
id: { type: 'string', columnName: '_id', required: true }, //<---- Add it here
name: {
type: 'string',
required: true
},
......
}

Migration doesnt work with sails JS and postgresql db

I just joined a project using SailsJS. I am trying to run a migration on the user model (I want to add a confirmed column boolean). I proceeded this way :
1) I modified the user model in models/User.js adding the confirmed attribute. Here's the model User.js :
attributes: {
id: {
type: 'int',
primaryKey: true,
autoIncrement: true
},
confirmed: {
type: 'boolean',
required: true,
defaultsTo: false
},
facebook_id: {
type: 'int'
},
email: {
type: 'string',
unique:true,
required: true
},
password:{
type: 'string'
// ,
// password: true
},
password_confirmation:{
type:'string'
},
token: {
type:'string',
size: 2000
},
first_name: {
type: 'string',
required: true
},
last_name: {
type: 'string',
required: true
},
full_name: {
type: 'string'
},
poste: {
type: 'string'
},
favorite_club: {
type:'string'
},
picture: {
type:'string',
defaultsTo: 'https://wefoot.s3.amazonaws.com/default.svg'
},
birthday: {
type: 'date'
},
mangoId: {
type: 'integer'
},
telephone: {
type: 'string'
},
password_reset_token:{
type:'string'
},
last_seen:{ //Last opening of notif page
type:'datetime'
},
pending_notif:{
type:'int',
defaultsTo: 0
},
nb_connection:{
type:'int',
defaultsTo: 0
},
last_lat:{
type: 'float'
},
last_long:{
type: 'float'
},
2) In config/env/development.js I changed migrate: safe to migrate alter :
module.exports = {
models: {
connection: 'postgresLocal',
migrate: 'alter'
}
};
3) I ran sails lift and changed back development.js to migrate safe
But When I checked in Pgadmin, the new confirmed column is not showing up and I get this error in my logs
Unhandled rejection Error (E_VALIDATION) :: 1 attribute is invalid
at WLValidationError.WLError (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:26:15)
at new WLValidationError (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js:20:28)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:46:43
at allValidationsChecked (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:210:5)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:49:16
at done (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:239:19)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:40:16
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:191:23
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:49:16
at done (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:239:19)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:40:16
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:164:64
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:162:20
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:230:13
at _arrayEach (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:81:9)
at _each (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:72:13)
What am I doing wrong here ?

Can I use primary key in association for find Waterline

I've created two models with associations:
User.js
attributes: {
fullname: {
type: 'string',
required: true
},
username: {
type: 'string',
unique: true,
required: true
},
email: {
type: 'email',
unique: true,
required: true
},
mothertongue: {
type: 'string'
},
passports: {
collection: 'Passport',
via: 'user'
},
words: {
collection: 'Words',
via: 'owners',
dominant: true
}
}
Words.js
attributes: {
word: {
type: 'string',
unique: true,
required: true
},
language: {
type: 'string',
required: true
},
owners: {
collection: 'User',
via: 'words'
}
}
When I'm searching about words owner someone he return back empty array
Words
.find({
owners: req.session.passport.user
})
.exec(function(err, data){
if (err) {
return res.send('error');
}
res.send(data);
})
Also I used .populate('owners') but don't work too.
To find the words a user has:
User.find(1).populate('words')
To find which users own a particular word, use
Word.find(id).populate('owners', {id : ...})

Sails.js model association

Currently my two models look like this
module.exports = {
tableName: 'accounts',
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true
},
name: {
type: 'string',
required: true
},
password: {
type: 'string',
required: true
},
email: {
type: 'string',
required: true
},
gang_name: {
type: 'string',
required: true
},
family_id: {
type: 'string',
required: true
},
world: {
type: 'string',
required: true
},
messages: {
collection: 'Messages',
via: 'for'
}
}
}
And my Messages model
module.exports = {
tableName: 'messages',
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true
},
title: {
type: 'string',
required: true
},
text: {
type: 'string',
required: true
},
for: {
model: 'Accounts',
required: true
},
by: {
type: 'integer',
required: true
}
}
};
I want to associate the for field of a message with an account so if 'for' field is = 11 load account with id 11... Currently im trying this way
Accounts.find({ id: req.session.accountid }).populate('Messages').exec(function(err, data) {
console.log(data, err);
});
But Im getting an error
Attempting to populate an attribute that doesnt exist
You've got to use the populate method with the attribute name ('messages'), not the model name ('Messages').