我扩展了一个Parse.Object
.现在我想在保存对象之前取消设置嵌套属性。此嵌套属性settings
是一个对象。
所以我的钩子看起来像这样:
Parse.Cloud.beforeSave('MyObject', req => {
if (req.object.get('settings').propertyToUnset) {
req.object.unset('settings.propertyToUnset');
}
});
但是,这会产生错误Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
unset
如何用于嵌套属性?
你可以尝试这样的事情:
Parse.Cloud.beforeSave('MyObject', async req => {
const settings = req.object.get('settings');
await settings.fetch();
if (settings.get('propertyToUnset')) {
settings.unset('propertyToUnset');
await settings.save();
}
});
解决方案是使用普通的JavaScript命令delete
删除嵌套对象的属性settings
:
Parse.Cloud.beforeSave('MyObject', req => {
if (req.object.get('settings').propertyToUnset) {
delete req.object.get('settings').propertyToUnset;
}
});