delete object.property不适用于Mongoose Created Object

时间:2015-12-22 23:49:37

标签: javascript node.js mongoose

我正在使用Mongoose在数据库中创建用户模型。当我将用户模型返回到浏览器时,我想删除某些字段,例如Salt和Password Hash。

我正在使用Javascript delete关键字,但它没有删除这些属性。

我写了两个例子。

示例1(Works):使用Salt创建用户对象,删除盐

describe('Manual User Object', function () {

  it('should delete salt', function (done) {

    // MANUALLY CREATE JAVASCRIPT OBJECT
    var entity = { name: 'david', 'salt': 'yes please' };

    l.kv('typeof entity', typeof entity);
    l.kv('typeof entity.salt', typeof entity.salt);

    l.line('before delete');
    l.inspect(entity);
    l.info();

    l.line('delete entity.salt');
    var isDeleted = delete entity.salt;
    l.kv('Is Successful', isDeleted );
    l.info();

    l.line('after delete');
    l.inspect(entity);

  });

});

示例2(不工作):使用Salt在Mongoose中创建的用户对象

创建用户控制器功能

// ------------------------------------------------------------
// CREATE: Creates a new user in the DB.
// ------------------------------------------------------------
exports.create = function (req, res) {

  //var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
  //console.log(fullUrl);

  try {

    UserSchema.create(req.body.entity, function (err, entity) {

      if (err) {
        return mong.handleError(res, err);
      }

      // Fails to remove Salt
      entity = santitise(entity);

      return res.status(201).json(mong.formatSuccess(entity));
    });

  } catch (e) {
    console.log(e);
  }
};

单元测试失败   它('应该创建一个' + modelName,function(done){

var payload = td.users.newEntity('David', 'david@test.com', 'orderer', 'password');

request(app)
    .post('/api/users')
    .send(mong.wrapEntity(payload))
    .expect(201)
    .expect('Content-Type', /json/)
    .end(function (err, res) {

      if (err) {
        l.logTheUnknown(err);
        return done(err);
      }

      var document = res.body;
      var entity = document.entity;

      l.logDocument(document);
      logEntity(entity);

      utest.expectSuccess(document);
      utest.expectTimeStampsOnCreate(entity, modelName);

      // Check properties are returned
      entity.should.have.property('name', 'David');
      entity.should.have.property('email', 'david@test.com');
      entity.should.have.property('role', 'orderer');
      entity.should.have.property('provider', 'local');

      //entity.should.have.property('password', 'david@test.com');
      //entity.should.not.have.property('hashedPassword');
      entity.should.not.have.property('salt');

      return done(err);
    });

});

消毒功能     //这个实体已经从MONGOOSE.CREATE传来     功能santitise(实体)     {

  l.kv('typeof entity', typeof entity);
  l.kv('typeof entity.salt', typeof entity.salt);

  l.line('before delete');
  l.inspect(entity);
  l.info();

  l.line('delete entity.salt');
  var isDeleted = delete entity.salt;
  l.kv('Is Successful', isDeleted);
  l.info();

  l.line('after delete');
  l.inspect(entity);

  return entity;
}

LOG

1 个答案:

答案 0 :(得分:1)

感谢JohnnyHK,

解决方法是根据以下内容将清理更改为以下内容:why-cant-i-delete-a-mongoose-models-object-properties

function sanitise(entity)
{
  var result = entity.toObject();

  delete result.hashedPassword;
  delete result.salt;

  return result;
}