【发布时间】:2019-07-23 21:03:54
【问题描述】:
我正在使用节点并且我已经使用过。
babel 节点
"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
【问题讨论】:
-
不,它看起来非常好,到目前为止,这是您的数据对象的副本,而不仅仅是您期望的用户对象;)我在这里猜测传播运算符全部取用,而原始用户对象仅显示要枚举的几个属性
-
我在创建用户时使用了相同的方法。它在那里工作。可能 user.create 和 user.find 的返回数据不同?
标签: javascript node.js spread-syntax