I am setting up seed data with the goal of only creating a record if it does not already exist.
I am using the https://www.npmjs.com/package/mongoose-find-one-or-create plugin to only create records if they do not exist and it is not working.
I have also debugged into their code and written my own tests doing the following.
- Create Record - Name = 'Abc'
- Find Record where Name = 'Abc'
- if found
- print found
- if NOT found
- Create Record - Name = 'Xyz'
I was expecting to see the message 'Found', but instead I am getting a new record called 'Xyz'
I have a feeling it maybe some sort of asynchronous issue but I am new to mongo so not totally sure.
Here is all my test code, plus the code from the plugin.
Scheme Definition for Account Table
'use strict';
var mongoose = require('mongoose');
var findOneOrCreate = require('mongoose-find-one-or-create');
var Schema = mongoose.Schema;
var AccountSchema = new Schema({
name: String,
type: String,
notes: String,
locationIds: String,
});
AccountSchema.plugin(findOneOrCreate);
module.exports = mongoose.model('Account', AccountSchema);
Seed File for creating new rows
'use strict';
var Account = require('../../api/account/account.model');
Account.create({ name: 'Abc' }, function (err, small) {
});
Account.findOne({ name: 'Abc' }, function (err, row) {
console.log("Inside Find One");
if (row) {
console.log("Found: Yes");
console.log(row);
} else {
console.log("NOT FOUND");
Account.create({ name: 'Xyz' }, function (err, small) {
});
}
});
The FindOneOrCreatePlugin code
'use strict';
/**
* Mongoose Plugin: findOneOrCreate
* Copyright(c) 2014 Mohammad Khan <mohammad.khan@gmx.us>
* MIT Licensed
**/
function findOneOrCreatePlugin(schema) {
//console.log('findOneOrCreatePlugin: SETUP');
schema.statics.findOneOrCreate = function findOneOrCreate(condition, doc, callback) {
var self = this;
//console.log('CONDITION');
//console.log('----------------------------------------------------------------------------------------------------');
//console.log(condition);
//console.log('----------------------------------------------------------------------------------------------------');
self.findOne(condition, function(err, result ) {
//console.log('ERROR');
//console.log('----------------------------------------------------------------------------------------------------');
//console.log(err);
//console.log('----------------------------------------------------------------------------------------------------');
//console.log('RESULT');
//console.log('----------------------------------------------------------------------------------------------------');
//console.log(result);
//console.log('----------------------------------------------------------------------------------------------------');
if (result) {
//console.log('----------------------------------------------------------------------------------------------------');
//console.log('YEEEEEEEEEEEEEEY an UPDATE');
//console.log('----------------------------------------------------------------------------------------------------');
callback(err, result);
} else {
//console.log('CREATE');
//console.log('----------------------------------------------------------------------------------------------------');
//console.log(doc);
//console.log('----------------------------------------------------------------------------------------------------');
self.create(doc, function(err, result) {
callback(err, result);
});
}
});
};
}
module.exports = findOneOrCreatePlugin;
Query in Mongo Shell
Console Output in Web Server
**Here is the use case that I'm attempting to deal with **
NOTE: this is part of a set of files generated by end users with a
The USE case I was attempting to resolve was if the user accidently put the same account in twice.
This code is essentially meant to be self-healing when users accidently put duplicates in, it is only a seed system for populating a new application.
I do plan to use this on the product table which has 1000's of products.
According to @RobertMoskal, I need to put the calls inside of call back functions but then I'll have huge nested callbacks, 1 for each product.
// Create account called 'Chicane'
Account.findOneOrCreate(
{ name: 'Chicane'},
{
name: 'Chicane',
type: 'Orderer',
},
function (err, account) {
console.log('Account.findOneOrCreate');
console.log(account);
}
);
// Create account called 'Campos'
Account.findOneOrCreate(
{ name: 'Campos'},
{
name: 'Campos',
type: 'Supplier',
},
function (err, account) {
console.log('Account.findOneOrCreate');
console.log(account);
}
);
// Create account called 'Chicane', this has already been created so it should NOT be created again, but it is
Account.findOneOrCreate(
{ name: 'Chicane'},
{
name: 'Chicane',
type: 'Orderer',
},
function (err, account) {
console.log('Account.findOneOrCreate');
console.log(account);
}
);