简体   繁体   English

如何在Node中刷新对象的所有属性

[英]How to flash all the properties of an object in Node

I'm using Express with connect-flash. 我正在使用带有连接闪存的Express。

In validating a form, I am currently using this and it works: 在验证表单时,我目前正在使用它并且可以正常工作:

req.flash('old_name', object.name)
req.flash('old_email', object.email)
req.flash('old_age', object.age)

How can I iterate through the properties of the object and flash for each? 如何遍历对象的属性并为每个对象闪烁? I'd like to be able to reuse the solution to validate other objects. 我希望能够重用该解决方案来验证其他对象。

Something like: 就像是:

for (let property in object) {
    req.flash('old_' + property, object[property])
}

console.log(object) : console.log(object)

{
    _id: 'sjdfoiasdhfaushdfhweuhu',
    name: 'Harry',
    email: 'harry@example.com',
    age: 45
}

I figured this out. 我想通了。 I think this solution will be pretty specific to Express.js with mongoose: 我认为此解决方案将非常适合于Mongoose的Express.js:

for (var prop in object._doc) {
    if (object._doc.hasOwnProperty(prop)) {
        req.flash('old_' + prop, object._doc[prop])
    }
}

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

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