Sails.js 无法重置sailsjs中的模型实例属性

Sails.js 无法重置sailsjs中的模型实例属性,sails.js,Sails.js,在我们开始之前:我用种子数据和两个REST端点制作了一个非常小的git回购,在这里测试这个问题: 因此,我有三种模式:预约,它有许多程序,一对一程序。 由于帆上没有嵌套的种群,我用procedureItem手动获取procedureItem,方法如下: Appointment.find(1).exec(function(err, appointments){ if(err) return res.negotiate(err); async.eachSeries

在我们开始之前:我用种子数据和两个REST端点制作了一个非常小的git回购,在这里测试这个问题:

因此,我有三种模式:
预约
,它有许多
程序
,一对一
程序。
由于帆上没有嵌套的种群,我用
procedureItem
手动获取
procedureItem
,方法如下:

  Appointment.find(1).exec(function(err, appointments){
        if(err) return res.negotiate(err);
        async.eachSeries(appointments, function(appointment, cb){
            Procedure.find({appointment: appointment.id}).populate('procedureItem').exec(function(errP, procedures){
                if(errP) return cb(errP);
                appointment.procedures = procedures;
                appointment.proceduress = procedures;
                cb()
            })
        }, function(errE){
            if(errE) return cb(errE);
            res.ok(appointments)
因此,问题是当我想用新的数组过程(具有我需要的嵌套深度)替换
procedures
属性时,它只是没有设置,如果你点击演示的端点,就没有
procedures
属性可用。
作为测试,我将相同的新过程数组附加到
procedures
(双s)属性,并正确设置此属性。
我尝试过使用
.toObject()
方法,但没有成功。
您尝试了sails 0.10.5和0.11.0,但运气不佳。

是的,没有过程属性,因为约会有很多过程,所以约会表中的DB中没有字段过程。因此,如果您不喜欢:

Appointment.find(1).populate('procedures').exec(function(err, appointments){
.....
}

约会对象中将不会有属性过程

我刚才遇到了完全相同的问题,我相信这是因为模型正在使用“过程”属性,无法设置。我最终克隆了每一行,然后就可以设置值了

要更改代码,请执行以下操作:

if(err) return res.negotiate(err);
        var toreturn=_.map(appointments, function(a){
            var b=_.clone(a); // so you clone it to a new obj, then you can set the procedure... 
            b.procedures = 'aaaa'
            return b;
        })
        //res.json(toreturn); // Here you will get appointments with procedures='aaa'
        async.eachSeries(toreturn, function(appointment, cb){
            Procedure.find({appointment: appointment.id}).populate('procedureItem').exec(function(errP, procedures){
                if(errP) return cb(errP);
                appointment.proceduress = procedures;
                appointment.procedures = procedures;
                cb()
            })
        }, function(errE){
            if(errE) return cb(errE);
            res.ok(toreturn)
        })
我不知道为什么会发生这种情况,但这只是一个解决办法

顺便说一句,我建议您不要对每个指定执行异步,而是对每个指定执行一个查询,执行两个大查找,然后循环合并它们

大概是这样的:

var globalApps=[];
Appointment.find().then(function(apps){
    globalApps=apps;
    var appIDs=apps.map(function(a){return a.id}); //Get all appointment IDs
    return Procedure.find({appointment:appIDs}).populate('procedureItem');
}).then(function(procs){ //This procs is all procedures match the ids
    var toReturn =_.map(globalApps,function(a){
        var b=_.clone(a);
        b.procedure=_.find(procs,{appointment:b.id}); //  This is O(n^2) complexity matching, but you can do something smart.
       return b;
    }); 
    // Now you have toReturn as the appointment array.
   return res.json(toReturn);
})

我以前在这个问题上花了很多时间,很高兴有人在这里遇到了同样的问题。您遇到的问题是
procedures
属性不是一个简单的Javascript变量;它有一个“”和一个“”,这是使这些功能正常工作所必需的:

appointment.procedures.add(<some Procedure obj or id>);
appointment.procedures.remove(<some Procedure id>);

请注意,这里可以进行大量优化——检索约会后,您可以取出所有
ProcedureItem
ID,一次查找所有
ProcedureItem
实例,然后将它们映射到过程中,从而节省大量查询

当然。问题是,当我试图手动设置procedures属性时,它就是无法设置。这就是为什么我如此困惑。请仔细阅读问题或尝试演示。我很感激有时间回复,但您的回答与问题无关。我看不出有问题,一切正常:。无论如何,我有三个问题:1。你使用什么版本的帆?在你的回购协议中,这是0.10.5,为什么?2.为什么您不简单地请求像
Procedure.find({appointment:1}).populate('procedureItem')
?3.为什么在其中使用异步数据库请求?为何如果您有1000条记录并向DB生成1000条请求,该怎么办?你有
if(errE)返回cb(errE)如果errE不为null,将生成异常。关于异步。如果您需要请求约会次数,可以使用where语法,如
Procedure.find({appointment:[1,2,3]})。填充('procedureItem')
并在一个请求中获取该约会的所有过程非常感谢您尝试演示。如果我看到您的结果,我会看到有
procedures
(双s)和
proceduress
(三s),但没有
过程
。(这是我愚蠢的错误,因为演示是这样的…你能拉大师吗?)至于你的Qs,我使用的是sails v0.10.5。2.我这样做的原因是我无法将其附加到主要对象上。3.我将研究改进方法。@JuanSolano事实上,我所做的只是
git clone
,然后
npm install
,以获得缺少的软件包。然后
帆升起
,根本不写字符串。所以我很困惑。Node和Npm的版本可能存在某种问题?我也很高兴其他人有这个问题。我不想走你的路,但显然这是唯一的路。感谢您的回答和有关异步的提示。我希望这能在赛尔斯得到解决。关于模型正在使用的程序。。。这不是使用toObject()的目的吗