Problems exporting model functions with Mongoose - node.js

I'm struggling with creating model functions for Mongoose models. I define a method here:
Schema.listingSchema.method('applyPrice', function() {
this.price = priceFromString(this.title);
});
and I access it here:
var listing = new Listing();
// assign all relevant data
listing.title = title;
...
// pull the price out of the title and description
listing.applyPrice(listing);
where
Listing = mongoose.model('Listing', Schema.listingSchema);
and I receive the error:
TypeError: Object #<model> has no method 'applyPrice'
Can anyone see the issue?

How are you defining your schema? Usually you would do something like this:
var listingSchema = new mongoose.Schema({
title: String
});
listingSchema.method('applyPrice', function() {
this.price = priceFromString(this.title);
});
mongoose.model('Listing', listingSchema);
var Listing = mongoose.model('Listing');
var listing = new Listing({ title: 'Title' });
listing.applyPrice();

Related

Mongoose Bulk upsert not working

Hi i am new to nodejs and mongoose. Just trying to update mongo data with bulk inserts with following methods.
"use strict";
var mongo = require('../models/tracking_mongo');
var Schema = mongo.trackingMasterMongoosePoc.Schema;
//create a schema
var userwise_tracking_events = new Schema({
activities : {},
summary : {},
userId : Number,
counter : Number,
created : Date,
modified : Date
});
let collection = 'userwise_tracking_events';
let UserwiseTrackingEvents = mongo.trackingMasterConnPoc.model(collection, userwise_tracking_events);
UserwiseTrackingEvents.updateCollectionStream = function(condition, params, options, callback){
var Bulk = UserwiseTrackingEvents.collection.initializeUnorderedBulkOp();
Bulk.find(condition).upsert().update(params);
Bulk.execute(callback);
};
module.exports = UserwiseTrackingEvents;
Upper code works fine but this didn't solved my problem for using bulk inserts.
Then i just made a change by making the bulk variable global.
"use strict";
var mongo = require('../models/tracking_mongo');
var Schema = mongo.trackingMasterMongoosePoc.Schema;
//create a schema
var userwise_tracking_events = new Schema({
activities : {},
summary : {},
userId : Number,
counter : Number,
created : Date,
modified : Date
});
let collection = 'userwise_tracking_events';
let UserwiseTrackingEvents = mongo.trackingMasterConnPoc.model(collection, userwise_tracking_events);
var Bulk = UserwiseTrackingEvents.collection.initializeUnorderedBulkOp();
UserwiseTrackingEvents.updateCollectionStream = function(condition, params, options, callback){
Bulk.find(condition).upsert().update(params);
Bulk.execute(callback);
};
module.exports = UserwiseTrackingEvents;
Now getting the error find of undefined is not a function.
When i checked
console.log(UserwiseTrackingEvents.collection);
Gave me result with NativeCollection.collection is null.
Do let me know what i am doing wrong.
I have another methods to work on this but i specifically want what i am doing wrong in this question.
Thanks :)
I would venture to say that your declaration of Bulk is not outside of any function declaration when moved out of the function call. Thus when the exposed function is called , Bulk was not initialized.

Mongoose Plugin - schema.add to Subschema

I am writing a mongoose plugin that involves adding a elements to the subschema. I was thinking it would be quite straightforward, but I've been trying unsuccessfully for a few days now.
Below is a simplification of the schema:
var inheritables = require('./inheritables.server.module.js');
var OuFieldTypeSchema = new Schema({
name: String,
desc: String
});
var OuFieldType = mongoose.model('OuFieldType', OuFieldTypeSchema);
var OuSchema = new Schema({
name: String,
fieldTypes: [OuFieldTypeSchema],
});
OuSchema.plugin(inheritables, {inherit: ['fieldTypes']});
mongoose.model('Ou', OuSchema);
Within the body of my Mongoose Plugin, I'd like to iterate over the "inherit" array and add the following to the schemas that represent each of these elements in the Ou schema;
A simplification of my plugin is as follows:
var _ = require('lodash');
module.exports = exports = function(schema, options) {
_.each(options.inherit, function(currItemSchema){
var addToSchema = {inheritedFrom: {name: String}};
//Really want to add this to the OuFieldTypeSchema represented by the fieldTypes element array.
schema.add(addToSchema, currItemSchema + ".");
});
});
Note that the second parameter of add() adds a prefix, but this doesn't work.
I've also tried:
_.each(options.inherit, function(currItemSchema){
var addToSchema = {};
addToSchema[currItemSchema] = {inheritedFrom: {name: String}};
schema.add(addToSchema);
});
The expected behaviour here is that elsewhere in my plugin, I have a method that sets the inheritedFrom schema. No error is received when the item is set, but the resulting output never sees the inheritedFrom elements.
I am currently adding inheritedFrom to each subschema that is used by OuFieldType, but the above is a simplification and is not very DRY.
I'm new to Node.JS - any pointers in the right direction would be greatly appreciated.

mongoose, getting object's id after using collection.insert

I'm trying to get the object id after adding it to the db (using collection.insert)
mongoose.model('Persons').collection.insert(person, function(err, newPerson) {
console.log('lets see you', newPerson);
});
and from the console I'm getting only result: { ok: 1, n: 1 } in stand of the new obj, any ideas how can I rich to the new object ?
thanks!
You can use save() here
var Persons = mongoose.model('Persons');
var personJSON = {
..... // persons schema values you want to insert
};
var person = new Persons(personJSON);
var result = yield person.save();
result variable will contain all the fields you inserted along with _id

Why are mongoose references turning into IDs?

According to http://mongoosejs.com/docs/populate.html, if I set a ref property to an object, and not an ID, when getting it, I should get back an object and not an ID. I'm referring to this part of the page:
var guille = new Person({ name: 'Guillermo' });
guille.save(function (err) {
if (err) return handleError(err);
story._creator = guille;
console.log(story._creator.name);
// prints "Guillermo" in mongoose >= 3.6
// see https://github.com/LearnBoost/mongoose/wiki/3.6-release-notes
Here's my sample code:
var T1schema = new mongoose.Schema({
otherModel:{type:mongoose.Schema.Types.ObjectId, ref:"T2"}
});
var T1 = mongoose.model('T1', T1schema);
var T2schema = new mongoose.Schema({
email: {type: String},
});
var T2 = mongoose.model('T2', T2schema);
var t1 = new T1();
var t2 = new T2({email:"foo#bar.com"});
t1.otherModel = t2;
Now when I refer to t1.otherModel, it's just an ObjectId, and not a T2. For example:
console.log(t1.otherModel.email);
prints undefined instead of "foo#bar.com". Any ideas why? Note I'm using Mongoose 3.6.18 according to it's package.json.
Thanks!
I think your expectations here just don't match what mongoose does. The schema is a way of saying "model M's property P will always be of type T". So when you set a value, mongoose uses the schema definitions to cast the set value to the type the schema requires. Here's a little REPL session. Note setting a number property with a string value casts it to a number, but trying to store a boolean in a number field just ignores the errant value.
> var mongoose = require('mongoose')
> var schema = new mongoose.Schema({prop1: Number})
> var Model = mongoose.model('Model', schema)
> var m = new Model
> m.prop1 = 42
42
> m.prop1
42
> m.prop1 = "42"
'42'
> m.prop1
42
> m.prop1 = false
false
> m.prop1
42
So when your schema says something is going to be an ObjectId, if you give it a model instance, mongoose immediately converts it to an ObjectId in preparation for a write to the database, which is the common case. Normally if you just set that model instance, you don't need to get it back out of the parent model before saving the parent model to the database.
So the model instance getter defined properties are always going to return something compatible with the schema, and populate has to do with loading refs from the DB, but for whatever reason mongoose just doesn't work the same comparing a .populated instance with a non-populated instance. I can see why this is confusing and perhaps unexpected/disappointing though.
Mongoose is normalizing the instance to match the Schema, which specifies that otherModel is an ObjectId.
otherModel:{type:mongoose.Schema.Types.ObjectId, ref:"T2"}
So, Mongoose treats t1.otherModel = t2; the same as:
t1.otherModel = t2._id;
The ref isn't used until .populate() is called (either directly on the document or in a query), which needs both objects to be saved:
t2.save(function (err) {
t1.save(function (err) {
console.log(t1.otherModel);
// 7890ABCD...
t1.populate('otherModel', function () {
console.log(t1.otherModel);
// { email: 'foo#bar.com', _id: 7890ABCD..., __v: 0 }
});
});
});

NodeJS Module Pattern

I'm about to begin writing a new module for a system I'm developing. We use a MySQL database (so I'm using node-mysql) which contains a customers table.
What I want to achieve is:
Outside of the module I'm looking to do var C = new Customer(1) where 1 is the customer ID.
Now when I want to get something from this customer, I can do C.email or C.first_name which will simply return a value.
I also need to be able to set values back on this customer, C.email = 'example#example.com' or perhaps:
C.set('email', 'example#example.com')
What would be the best pattern to create such a model?
I already have something like this... Not exactly what you demanded but very close to that
I have generalized the core part and here is the code..Hope this will help....
var mysql = require('mysql');
var con = mysql.createConnection({
host:"yourHostName",
user:"yourUserName",
password:"yourPassword"
});
con.query("use databaseName");
function getCustomerDetails(custId){
con.query("select * from customer where custId = "+custId,function(err,result,fields){
if(!err)
return result;
else
console.log(err);
});
}
function updateCustomerDetails(custId,fieldName,fieldValue){
con.query("update customer set "+fieldName+" = "+fieldValue+" where custId = "+custId,function(err,result,fields){
if(!err)
return true;
else
console.log(err);
return false;
});
}
exports.getCustomerDetails = getCustomerDetails;
exports.updateCustomerDetails = updateCustomerDetails;
And then suppose you saved the module as dbAccessModule.js Then you can use the functions like this
var C = require('./dbAccessModule');
result = C.getCustomerDetails(1);
console.log(result.fieldName);
var success = C.updateCustomerDetails(1,'name','sumit');
if(success)
console.log('Table Updated successfully....');
else
// take necessary action according to your application
One thing you need to take care of is that if you are updating any field with string value
then please don't forget to surround the value of fieldValue with single quotes.
If this is not what you asked for then please ignore it....
I recently created two database modules you might be interested in checking out to see if they fit your needs - an ORM: http://bookshelfjs.org and Query Builder: http://knexjs.org
The ORM is based off of the design patterns of Backbone.js
So, you'd be able to do something like this:
// Create the base customer object
var Customer = Bookshelf.Model.extend({
tableName: 'customers'
});
// Create a new customer instance with an id of 1, fetch it, and then
// act on the result model 'customer'.
new Customer({id: 1}).fetch().then(function(customer) {
console.log(customer.get('name'))
customer.set('email', 'email#example.com')
return customer.save();
});
You could also extend the base Customer class to enable a shortened syntax, similar to what you're looking for:
// Create the base customer object, with a static findOne method.
var Customer = Bookshelf.Model.extend({
tableName: 'customers'
}, {
find: function(id) {
return new this({id: id}).fetch();
}
});
Customer.find(1).then(function(C) {
console.log(C.get('name'))
C.set('email', 'email#example.com')
});

Resources