【问题标题】:Ember: The response to store.query is expected to be an array but it was a single record ErrorEmber:对 store.query 的响应应该是一个数组,但它是一条记录错误
【发布时间】:2017-02-05 00:14:18
【问题描述】:

我不断收到错误:

错误:断言失败:对 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);
});

}

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

谢谢。

【问题讨论】:

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


    【解决方案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
        });
    }
    

    【讨论】:

    • 我把它从查询改成queryRecord,但是console.log(records123)返回null。
    • 您确定正在找到学生吗?
    • @Jackie,您的帖子中有 router.route('/') 但您的代码将在前端“高级”,那么您的路线是否正确?
    • 是的,正在找到一个学生。服务器日志显示高级排名列表。以下是从我的 console.log(advancedStandings) 返回的众多高级排名中的一个;在我的后端 { _id: 5896457ed246a62f45315f6b, course: 'sdfsf', description: 'sdfsdf', units: 'sdfsdf', grade: null, from: 'sfdfs', studentInfo: 585df32e0bf2ba5ea6951592, __v: 0 }任何意义,因为我现在只是在测试。
    • 查看您的网络标签。实际的反应是什么?
    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2022-10-24
    • 1970-01-01
    相关资源
    最近更新 更多