I am currently working on a project which is using the loopback framework. It is extremely frustrating in I feel as if I am fighting with this framework to get something extremely simple done. I simply need to create newly created associated instances when a request is received to create a parent instance with information for the children. I have gotten it working to the point where it does in fact create the children but when I try to set the property so I can return both the parent and children in the response... it doesn't actually set the children property. What is going on here???
I can't find anything in the documentation about doing something simple like this. The most I can find is retrieval of nested models. I can't seem to find anything on creating them without sending 20 requests when I have a parent with many children.
module.exports = function(Parent) {
Parent.on('dataSourceAttached', () => {
const create = Parent.create.bind(Parent);
Parent.create = async (data, opts) => {
let instance;
try {
instance = await create(data);
if (data && data.children && Array.isArray(data.children)) {
const { id: parentId } = instance;
const children = await new Promise((resolve, reject) =>
instance.children.create(
data.children.map(m => Object.assign({}, m, { parentId })),
opts,
(err, res) => (err ? reject(err) : resolve(res))
)
);
instance.children = children;
console.log(instance.children === children); // false (wtf... #@#$#$%)
}
return instance;
} catch (err) {
// handle err
}
};
});
};
via Goblinlord
No comments:
Post a Comment