question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

default function in schema without context

See original GitHub issue

hi,

default function in the schema has currently no way to access the document.

var m = require('mongoose');

var schema = new m.Schema({
    a: {
        type: Number,
        default: function() {
            console.log(this);
        }
    }
});

var Model = m.model('mymodel', schema);

new Model();

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
lineuscommented, May 10, 2020

This was resolved in #6119.

1581.js

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
const { Schema } = mongoose;

const schema = new Schema({
  first: String,
  last: String,
  revName: String,
  fullName: {
    type: String,
    default: function() {
      if (allStrings(this.first, this.last)) {
        return `${this.first} ${this.last}`;
      }
    }
  }
});

schema.path('revName').default(function() {
  if (allStrings(this.first, this.last)) {
    return `${this.last}, ${this.first}`;
  }
});

const Test = mongoose.model('test', schema);

const test = new Test({ first: 'test', last: 'ing' });

assert.strictEqual(test.fullName, 'test ing');
assert.strictEqual(test.revName, 'ing, test');
console.log('All Assertions Pass.');

function allStrings() {
  let ret = true;
  [...arguments].map(arg => {
    if (typeof arg !== 'string') {
      ret = false;
    }
  });
  return ret;
}

Output

issues: ./1581.js 
All Assertions Pass.
issues: 
0reactions
sibouletcommented, Jan 24, 2018

Is there any plans on adding this? Not sure if thats expected or actually a bug, noticed this issue was opened back in 2013. Confirmed was an issue with 4.x and still an issue on 5.x.

var assert = require('assert');
var mongoose = require('mongoose');

var schema = new mongoose.Schema({
   title: String,
   arnold: Boolean,
});

schema.path('arnold').default(function() {
  return this.title && this.title.match(/arnold/i);
});

var BlogPost = mongoose.model('BlogPost', schema);

var post = new BlogPost({title: '5 Best Arnold Schwarzenegger Movies'});

assert.equal(post.arnold, true);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting schema name for DbContext - Stack Overflow
You can configure the default schema in OnModelCreating method of your custom inherited DbContext class like -
Read more >
Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in ... Apollo Server automatically defines a default...
Read more >
Documentation: 15: 5.9. Schemas - PostgreSQL
The first schema in the search path that exists is the default location for creating new objects. That is the reason that by...
Read more >
Entity Types - EF Core - Microsoft Learn
When using a relational database, tables are by convention created in your database's default schema. For example, Microsoft SQL Server will use ...
Read more >
Ecto.Schema — Ecto v3.9.4 - HexDocs
For example, you can use such schemas to receive data from a command line interface and validate it, without ever persisting it elsewhere....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found