繁体   English   中英

传播语法返回意外对象

[英]Spread syntax returns unexpected object

我正在使用node并且已经使用过。

巴别节点

    "start": "nodemon --exec babel-node --presets es2015 index.js"

我的传播语法无法正常工作。 这是我的代码。

   export const login = async (parentValue, { email, password }) => {
  try {
    const user = await User.findOne({
      email
    });
    console.log(user);

    if (!user.authenticateUser(password)) {
      throw new Error('Wrong password');
    }
    const dummyObject = {
      ...user
    };
    console.log({ dummyObject });
    return { ...user };
  } catch (e) {
    console.log(e);
    throw new Error(e.message);
  }
};

我使用console.log(user) ,它工作正常。 它返回{ id: xxx, name: xxxx }

而且我在console.log(dummyObject)上得到了意外的数据; 这就是我得到的。

{ jojo: 
{ '$__': 
      InternalCache {
        strictMode: true,
        selected: {},
        shardval: undefined,
        saveError: undefined,
        validationError: undefined,
        adhocPaths: undefined,
        removing: undefined,
        inserting: undefined,
        saving: undefined,
        version: undefined,
        getters: {},
        _id: 5c798295f53323b34cabf1ca,
        populate: undefined,
        populated: undefined,
        wasPopulated: false,
        scope: undefined,
        activePaths: [Object],
        pathsToScopes: {},
        cachedRequired: {},
        session: undefined,
        ownerDocument: undefined,
        fullPath: undefined,
        emitter: [Object],
        '$options': [Object] },
     isNew: false,
     errors: undefined,
     _doc: 
      { _id: 5c798295f53323b34cabf1ca,
        fullName: 'sarmad',
        password: '$2a$10$c.XDX75ORXYA4V/hUXWh.usVf2TibmKfY.Zpu3cpTssFaYvsGyhte',
        email: 'sarmad@gmail.com',
        createdAt: 2019-03-01T19:05:57.454Z,
        updatedAt: 2019-03-01T19:05:57.454Z,
        __v: 0 },
     '$init': true } }

难道我做错了什么? 从技术上讲,它应该返回用户对象注意:我不想使用Object.assign

看起来您正在使用猫鼬,并且看起来您正在通过使用spread运算符获得猫鼬对象属性。 您需要转换为JSON才能摆脱这些。

试试: const dummyObject = { ...user.toJSON() };

您还可以: const dummyObject = { ...user.toObject() };

^这可能是首选方式

另一种解决方案是仅在进行查询时请求普通对象。 例如:

Schema.findOne(query).lean()

这将返回一个普通对象而不是猫鼬对象。

您得到不同的日志,因为猫鼬使用自定义检查功能

在节点中尝试以下操作:

const obj = {
  [Symbol.for('nodejs.util.inspect.custom')]() {
    return "totally not an object";
  }
}

console.log(obj); // "totally not an object"

由于猫鼬inspect是在对象的原型上定义的,因此在您使用...时不会被复制,因为传播只会复制对象自身的属性。

class Obj {
  [Symbol.for('nodejs.util.inspect.custom')]() {
    return "totally not an object";
  }
}

const obj = new Obj();
const obj2 = { ...obj };


console.log(obj); // "totally not an object"
console.log(obj2); // {}

您可以通过将原型设置为复制对象来修复它:

Reflect.setPrototypeOf(obj2, Reflect.getPrototypeOf(obj))

但是由于您要处理自定义对象,因此不应真正使用对象传播。 价差仅对POJO安全。 否则,您可能会很容易陷入麻烦(使用隐藏的道具,吸气剂,安装员和原型地狱)

https://repl.it/repls/ToughModestInstructionset

https://github.com/Automattic/mongoose/blob/master/lib/document.js#L2853:L2869

https://nodejs.org/api/all.html#util_util_inspect_custom

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM