KeystoneJS:从版本 4 中的数据库填充条目时出错

KeystoneJS: Error populating entries from database in version 4

我最近 运行 在 KeystoneJS 0.3.22 中遇到了使用关系填充项目的问题。根据以下 post,这是版本 3 中的问题,但不是版本 4 中的问题。 升级到版本 4 后,从我的数据库中填充数据的前端表单似乎出现错误。

我得到的错误是:

    Sorry, an error occurred loading the page (500)

/home/dani/cns/templates/views/contact.pug:58
56| label Select the Clients that should be notified
57| select#selectboxgroups(name='groupSelect', multiple='multiple')
> 58| for record in records
59| option(value=record.groupname, selected=formData['record.groupname']) #{record.groupName}
60| .form-group
61| input(type='checkbox', name='smsmessage')

Cannot read property 'length' of undefined

下面的代码是我的初始化函数,包括填充记录的 Group.model.find。

view.on('init', function(next) {
        Enquiry.model.find().where('finalIncident', "No").select('incidentNumber updateNumber').exec(function(err,data){
            if(err)return console.log('error while fetching list of active incidents', err);
            var dataFiltered = getLatest(data);
            var incidents = [];
            for(var i=0; i<dataFiltered.length; i++){
                incidents.push({'incidentid': dataFiltered[i]._id, 'incidentNumber': dataFiltered[i].incidentNumber});
            }
            locals.incidents = incidents;
            //console.log(locals.incidents);
            return next();
        });
        Group.model.find().sort('name').exec(function(err,data){
            if(err)return console.log('error while fetching client list for contact page', err);
            var records = [];
            for(var x=0; x<data.length; x++){
                records.push({'groupid': data[x]._id, 'groupName': data[x].name });
            }
            locals.records = records;
            console.log(locals);
        });
    });

到目前为止,我还没有找到任何迹象说明为什么这会成为一个问题。我曾多次尝试更改使用的变量(我看到一个问题,即使用错误的单词和该单词的复数会导致问题)但没有成功。

有人知道我应该从哪里开始寻找吗?

您过早地致电 next;视图在您 运行 之前呈现您的数据库查询以获取您的 Groups。你应该将 Group.model.find() 调用放在你的 Enquiry.model.find() 查询中,或者使用像 async 这样的控制流库(我认为默认情况下它与 Keystone 一起安装)到 运行 两者并行运行,设置局部变量,然后渲染视图。假设您已经有 EnquiryGrouplocals...

等变量
var async = require('async');
view.on('init', function(next) {
    async.parallel([
        function (cb) {
            Enquiry.model.find().where('finalIncident', "No").select('incidentNumber updateNumber').exec(function(err,data){
                if(err) return cb('error while fetching list of active incidents', err);
                var dataFiltered = getLatest(data);
                var incidents = [];
                for(var i=0; i<dataFiltered.length; i++){
                    incidents.push({'incidentid': dataFiltered[i]._id, 'incidentNumber': dataFiltered[i].incidentNumber});
                }
                locals.incidents = incidents;
                //console.log(locals.incidents);
                return cb(null);
            });
        },
        function (cb) {
            Group.model.find().sort('name').exec(function(err,data){
                if(err)return cb('error while fetching client list for contact page', err);
                var records = [];
                for(var x=0; x<data.length; x++){
                    records.push({'groupid': data[x]._id, 'groupName': data[x].name });
                }
                locals.records = records;
                console.log(locals);
                return cb(null);
            });
        }
    ], function (err, results) {
        console.log(err);
        return next();
    });
});