Ember:对store.query的响应应该是一个数组,但它只是一个记录错误

时间:2017-02-05 00:14:18

标签: node.js mongodb ember.js ember-data ember-cli

我一直收到错误:

  

错误:断言失败:对store.query的响应应该是一个数组,但它只是一个记录。请将您的回复包装在一个数组中,或使用store.queryRecord查询单个记录。

这是我的后端:

router.route('/')
.post(parseUrlencoded, parseJSON, function (request, response) {
    var advancedStanding = new models.AdvancedStandings(request.body.advancedStanding);
    advancedStanding.save(function (error) {
        if (error) response.send(error);
        response.json({advancedStanding: advancedStanding});
    });
})
.get(parseUrlencoded, parseJSON, function (request, response) {
    //var l = parseInt(request.query.limit);
    //var o = parseInt(request.query.offset);
    var AdvancedStanding = request.query.filter;
    if (!AdvancedStanding) {
        models.AdvancedStandings.find(function (error, advancedStandings) {
            if (error) response.send(error);
            response.json({advancedStanding: advancedStandings});
        });
        //models.AdvancedStandings.paginate({}, { offset: o, limit: l },
        //    function (error, students) {
        //        if (error) response.send(error);
        //        response.json({advancedStanding: advancedStandings.docs});
        //    });
    } else {
        //        if (Student == "residency")
        models.AdvancedStandings.find({"studentInfo": AdvancedStanding.studentInfo}, function (error, advancedStandings) {
            if (error) response.send(error);
            console.log(advancedStandings);
            response.json({advancedStudent: advancedStandings});

        });
    }
});

这是我的前端:

showStudentData: function (index) {
this.set('currentStudent', this.get('studentsRecords').objectAt(index));
this.set('studentPhoto', this.get('currentStudent').get('photo'));
var date = this.get('currentStudent').get('DOB');
var datestring = date.toISOString().substring(0, 10);
this.set('selectedDate', datestring);

var self2 = this;
this.get('store').query('advanced-standing',{filter:{studentInfo:'585df32e0bf2ba5ea6951592'}})
.then(function(records123){
  console.log(records123);
});

}

有没有人知道这是怎么回事?

感谢。

1 个答案:

答案 0 :(得分:1)

query函数需要多个结果。所以你需要使用queryRecord

this.get('store').queryRecord('advanced-standing',{filter:{studentInfo:'585df32e0bf2ba5ea6951592'}})
.then(function(records123){
  console.log(records123);
});

或将您的回复包装在数组文字

if (!AdvancedStanding) {
    models.AdvancedStandings.find(function (error, advancedStandings) {
        if (error) response.send(error);
        response.json([{advancedStanding: advancedStandings}]);
        // you were returning a single object
        // note the array is now wrapping your previous result

    });
    //models.AdvancedStandings.paginate({}, { offset: o, limit: l },
    //    function (error, students) {
    //        if (error) response.send(error);
    //        response.json([{advancedStanding: advancedStandings.docs}]);
              // you were returning a single object
              // note the array is now wrapping your previous result
    //    });
} else {
    //        if (Student == "residency")
    models.AdvancedStandings.find({"studentInfo": AdvancedStanding.studentInfo}, function (error, advancedStandings) {
        if (error) response.send(error);
        console.log(advancedStandings);
        response.json([{advancedStudent: advancedStandings}]);
        // you were returning a single object
        // note the array is now wrapping your previous result
    });
}