【问题标题】:Sailjs/Waterline - Is there any way to get previously fetched objects?Sails Js/Waterline - 有没有办法获取以前获取的对象?
【发布时间】:2014-05-01 07:57:47
【问题描述】:

我相信我看到有人写过一种从模型对象中获取内存对象以减少数据库访问的方法。有人可以建议或分享文件吗?

我想要这样的东西:

ModelA.create({...}).populate('modelBs').exec(function(err, instanceA) {
    // Do some nested things here, for example:
    instanceA.prop1 = someNewValue;
    instanceA.save(function(err, instanceAModified) {
       // I want to call some method from instanceAModified, to get all 'modelB' objects here, without accessing the DB once more.
    });
});

提前谢谢你!

【问题讨论】:

    标签: model sails.js waterline


    【解决方案1】:

    我相信在最新版本的 Waterline 中,.save() 将在其回调中重新填充任何关联。但无论如何,您始终可以通过将它们保存在局部变量中来保留它们。假设您的意思是find 而不是上面的create(因为populate 对create 没有任何作用):

    ModelA.find({...}).populate('modelBs').exec(function(err, instanceA) {
        // Do some nested things here, for example:
        instanceA.prop1 = someNewValue;
        var modelBs = instanceA.modelBs;
        instanceA.save(function(err, instanceAModified) {
           // modelBs will still be available here.
        });
    });
    

    【讨论】:

    • 知道了。非常感谢!
    猜你喜欢
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2012-03-21
    • 2014-04-19
    相关资源
    最近更新 更多