Node.JS / Mongoose / Express -> Object has no method "findAll" - model-view-controller

I'm trying to pass a method from my model.js to my route.js.. And my route doesn't find any method ! I searched a solution and tested a lot of codes, without success.
I'm a beginner in Node so sorry if it's a stupid error.
This is a part of my code :
Route.js
var mongoose = require('mongoose');
var membersModel = new require('../models/MemberModel');
// Member list page
exports.list = function(req, res){
membersModel.findAll(function(err, docs){
res.render('list.jade', { title: 'My Registration App - Member list', member: docs });
});
};
MemberModel.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
// Open DB connection
var MemberSchema = new Schema({
id : ObjectId,
title : { type: String, required: true, enum: ['Mr', 'Mrs', 'Mme', 'Miss'] },
lastname : { type: String, required: true, uppercase: true, trim: true},
firstname : { type: String, required: true},
mail : { type: String, trim: true, index: { unique: true, sparse: true } },
date : Date
});
// ...
MemberSchema.method.findAll = function (callback) {
Members.find(function (err, member) {
callback(null, member)
});
};
var conn = mongoose.createConnection('mongodb://localhost/members');
var MyModel = conn.model('Members', MemberSchema);
var instanceMember = new MyModel;
module.exports = instanceMember;
Thanks for the time passed helping me.
If you want other informations, tell me !

I think you have a few problems here:
Your schema's instance methods should be defined on MemberSchema.methods (not .method).
A method like findAll that returns instances should be defined as a static method of the schema (on MemberSchema.statics) instead of an instance method.
You should be exporting MyModel, not a new MyModel instance of it as you are now. module.exports = MyModel;
route.js should not be using new in its require as you want the MyModel class to be available to the file, not an instance of it.

for the find function the syntax usually is
<modelname>.find({conditions},function(err, results){
//use the results
});
and if u don't have any specific conditions and want to find all, you can just give empty object in the conditions {}
<modelname>.find({},function(err, results){
//use the results
});
and for the schema, an example
const articleSchema = {
title: String,
content: String
};
// to create a new model named Article
const Article = mongoose.model("article",articleSchema);

Related

JSON schema validation with perfect messages

I have divided the data entry in a REST call in 4 parts. Data can be sent to REST call via:-
headers
query params
path params
request body
So in order to validate the presence of any key in any of the above 4 parts I have created a schema in this format. So if in case I have to validate anything in query params I will add the key 'query' and then add the fields inside that, that needs to be validated
const schema = {
id: 'Users_login_post',
type: 'object',
additionalProperties: false,
properties: {
headers: {
type: 'object',
additionalProperties: false,
properties: {
Authorization: {
type: 'string',
minLength: 10,
description: 'Bearer token of the user.',
errorMessages: {
type: 'should be a string',
minLength: 'should be atleast of 23 length',
required: 'should have Authorization'
}
}
},
required: ['Authorization']
},
path: {
type: 'object',
additionalProperties: false,
properties: {
orgId: {
type: 'string',
minLength: 23,
maxLength: 36,
description: 'OrgId Id of the Organization.',
errorMessages: {
type: 'should be a string',
minLength: 'should be atleast of 23 length', // ---> B
maxLength: 'should not be more than 36 length',
required: 'should have OrgId'
}
}
},
required: ['orgId']
}
}
};
Now, in my express code, I created a request object so that I can test the validity of the JSON in this format.
router.get("/org/:orgId/abc", function(req, res){
var request = { //---> A
path: {
orgId : req.params.orgId
},
headers: {
Authorization : req.headers.Authorization
}
}
const Ajv = require('ajv');
const ajv = new Ajv({
allErrors: true,
});
let result = ajv.validate(schema, request);
console.log(ajv.errorsText());
});
And I validate the above request object (at A) against my schema using AjV.
The output what I get looks something like this:
data/headers should have required property 'Authorization', data/params/orgId should NOT be shorter than 23 characters
Now I have a list of concerns:
why the message is showing data word in the data/headers and data/params/orgId even when my variable name is request(at A)
Also why not my errormessages are used, like in case of orgId I mentioned: should be atleast of 23 length (at B) as a message, even then the message came should NOT be shorter than 23 characters.
How can I show request/headers instead of data/headers.
Also, the way I used to validate my path params, query params, header params, body param, is this the correct way, if it is not, then what can be the better way of doing the same?
Please shed some light.
Thanks in advance.
Use ajv-keywords
import Ajv from 'ajv';
import AjvKeywords from 'ajv-keywords';
// ajv-errors needed for errorMessage
import AjvErrors from 'ajv-errors';
const ajv = new Ajv.default({ allErrors: true });
AjvKeywords(ajv, "regexp");
AjvErrors(ajv);
// modification of regex by requiring Z https://www.regextester.com/97766
const ISO8601UTCRegex = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?Z$/;
const typeISO8601UTC = {
"type": "string",
"regexp": ISO8601UTCRegex.toString(),
"errorMessage": "must be string of format 1970-01-01T00:00:00Z. Got ${0}",
};
const schema = {
type: "object",
properties: {
foo: { type: "number", minimum: 0 },
timestamp: typeISO8601UTC,
},
required: ["foo", "timestamp"],
additionalProperties: false,
};
const validate = ajv.compile(schema);
const data = { foo: 1, timestamp: "2020-01-11T20:28:00" }
if (validate(data)) {
console.log(JSON.stringify(data, null, 2));
} else {
console.log(JSON.stringify(validate.errors, null, 2));
}
https://github.com/rofrol/ajv-regexp-errormessage-example
AJV cannot know the name of the variable you passed to the validate function.
However you should be able to work out from the errors array which paths have failed (and why) and construct your messages from there.
See https://ajv.js.org/#validation-errors
To use custom error messages in your schema, you need an AJV plugin: ajv-errors.
See https://github.com/epoberezkin/ajv-errors

Sailjs with sails-mongo is enforcing type incorrectly. What am I doing wrong?

I have a sails app working with just a simple index and simple create (insert) to a mongo db. When I enter correctly typed data hard coded to be the type stated in the model, I get an error.
url insert err = [Error (E_VALIDATION) 1 attribute is invalid] Invalid attributes sent to urls:
• status
• Value should be a number (instead of "0", which is a string)
This is a very small, new project so not a lot of settings have been changed from default.
Since I have console.log in the create, I can see exactly what I' sending to the urls.create:
{ url: 'http://www.dina.com',
status: 0,
statusDate: '2016-11-19T19:46:10.804Z' }
I'm not doing anything to enforce type and it looks like I'm obeying type. Why am I getting error?
The model looks like:
// urls.js
module.exports = {
attributes: {
url : { type: 'string' },
status: { type: 'number'},
statusDate: {type: 'date'}
}
};
My config/models.js has schema set to false:
// config/models.js
module.exports.models = {
connection: 'DigitalOceanMongodbServer',
migrate: 'safe',
schema: false
};
My controller creates a new object with hard-coded status and statusDate of the correct type:
// urlsController.js
create: function (req, res) {
let url = req.body.url;
if(!url) return res.json({failure: 'empty url'});
let isValid = sails.validurl.isUri(url);
if(!isValid) return res.json({failure: 'url is not valid'});
let newObj = {
url: url,
status: 0, <---- obviously a number
statusDate: new Date().toISOString() <---obviously a date
}
console.log(newObj);
urls.create(newObj).exec(function createCB(err,created){
if (err){
return res.negotiate(err);
} else {
return res.ok(created);
}
});
}
Specify "integer" instead of "number" in the type of your model. I did not find "number" in the docs.
See: http://sailsjs.org/documentation/concepts/models-and-orm/attributes

GraphQL : Implementing windowed pagination for regular list

I'm trying to implement a windowed pagination using a "List". I don't need the cursor based solution with connections, because I need to show numbered pages to the user.
There are "User" and "Post" objects."User" has one-to-many relation to "Post".
Using graphql-js for schema,
here is my schema for userType and postType:
var userType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: globalIdField('User'),
posts: {
type: new GraphQLList(postType),
args: {
page:{
type: GraphQLInt,
defaultValue: 0
}
},
resolve: (_, args) => {
//code to return relevant result set
},
},
totalPosts:{
type: GraphQLInt,
resolve: () => {
//code to return total count
}
},
}),
interfaces: [nodeInterface],
});
var postType = new GraphQLObjectType({
name: 'Post',
fields: () => ({
id: globalIdField('Post'),
name: {type: GraphQLString},
//other fields
}),
interfaces: [nodeInterface],
});
Please notice the "totalPosts" field in "userType". Since there is going to be other Lists for the user,with the same paging needs, I'm going to end up maintaining lot of "total{Type}" variables in the fragment. This can be solved if I can send the totalCount within the List result somehow.
https://github.com/facebook/graphql/issues/4 this issue talks about implementing a wrapper over the List to include the totalCount in the result set.
I tried creating a wrapper like this:
var postList = new GraphQLObjectType({
name: 'PostList',
fields:()=>({
count: {
type: GraphQLInt,
resolve: ()=>getPosts().length //this is total count
},
edges: {
type: new GraphQLList(postType),
resolve: () => {
return getPosts() ; // this is results for the page, though I don't know how to use 'page' argument here
},
}
}),
interfaces: [nodeInterface],
});
but how should I connect this to the userType's posts field? And how can I use a 'page' argument on this wrapper, like I have in original userType?
how should I connect this to the userType's posts field? And how can I use a 'page' argument on this wrapper, like I have in original userType?
One simple way to implement what you're trying to do is to define a dumb wrapper type postList like this:
var postList = new GraphQLObjectType({
name: 'PostList',
fields:()=>({
count: { type: GraphQLInt },
edges: { type: new GraphQLList(postType) }
// Consider renaming 'edges'. In your case, it's a list, not a
// connection. So, it can cause confusion in the long run.
}),
});
Then in the userType definition, add a field of that wrapper type and define its resolve function like below. As for argument page, just describe it while defining the field type posts.
posts: {
type: postList,
args: {
page:{
type: GraphQLInt,
defaultValue: 0
},
...otherArgs
},
resolve: async (_, {page, ...otherArgs}) => {
// Get posts for the given page number.
const posts = await db.getPosts(page);
// Prepare a server-side object, which corresponds to GraphQL
// object type postList.
const postListObj = {
count: posts.length,
edges: posts
};
// Consider renaming 'edges'. In your case, it's a list, not a
// connection. So, it can cause confusion in the long run.
},
},

Custom validation of mongoose's Schema params

have the next params of object:
var mongoose = require (PATH);
var Schema = mongoose.Schema;
var schema - new Schema ({
barcode:{
type:number,
required:true,
unique:true
},...});
i want to validate 'barcode', which will be no less and no more than 14 characters;
for this i write the code below:
schema.path('barcode').validate(function(barcode){
return barcode.length == 13;
}, 'sorry, the error occurred, be careful while typing, 14 characters only!");
exports.Item = mongoose.model('Item', schema);
But when i implement this schema to the concrete object, this validation does not play any role. I mean, that i can type any length of number and no any error occur!
Checkout mongoose-validator. It integrates with mongoose to support custom validation. You can use it something like this.
var validate = require('mongoose-validator').validate;
var BarcodeSchema = new Schema({
code: {
type: String,
required: true,
unique: true,
validate: validate('len', 13)
}
});

BackboneJS and Model/Collection

Have json-data like this:
{ tournaments: [ {
tournament_id: "..."
tournament_name: "..."
events: [ {
event_id: ...
event_name: ....
param : [ {
param_a :
param_b : ..
subparan : [ {
sub_1: 1
sub_2 : 2...
So. I don't understand - how to it implement into BackBone Collection/Model style?
How to handle change sub_1? - Made Collection of Collection of Collection?
Simpliest way described in backbone tutorial:
var Events = Backbone.Collection.extend({
initialize: function(){
this.params = new Params()
}
})
var Tournaments = Backbone.Model.extend({
initialize: function(){
this.events = new Events()
}
})
var tournaments = new Tournaments()
You can continue nesting by you needs. When I was working on similar task I wrap each collection in model representing collection state and change itself in answer of collection events. This allows not to asking nested collections about its state having actual state in model.
var CollModel = Backbone.Model.extend({
defaults: {
state = ''//or list or dict or whatever
},
initialize: function(){
this.items = new Backbone.Collection();
this.items.on('all', this.setState, this)
},
setState: function(){
this.set(
'state',
this.items.reduce(function(state, item){
/*calculate state*/
}, '')
)
},
info: function(){
return this.get('state')
}
})
So you can nest collection-models with similar technic and read their state directly through instance.info() depends on how you calculate it. Your top model state will be updated from cascade updates of underneath models-collections.

Resources