Skip to content

Instantly share code, notes, and snippets.

@oakfang
Created April 9, 2017 08:57
Show Gist options
  • Save oakfang/c1a52d96192852375cfe745ad2664c40 to your computer and use it in GitHub Desktop.
Save oakfang/c1a52d96192852375cfe745ad2664c40 to your computer and use it in GitHub Desktop.
A function to ensure object id props of an object
import _ from 'lodash';
import { ObjectId } from 'mongoose';
import ensureObjectId from './utils';
exports default function ensureObjectIdProps(obj, paths) {
paths.forEach(prop => {
const currentValue = _.get(obj, prop);
_.set(obj, prop, currentValue ? ensureObjectId(currentValue) : new ObjectId());
});
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment