node.js - Ember : The response to store. 查询应该是一个数组,但它是一条记录错误

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

我不断收到错误:

Error: Assertion Failed: The response to store.query is expected to be an array but it was a single record. Please wrap your response in an array or use store.queryRecord to query for a single record.

这是我的后端:

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);
});

}

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

谢谢。

最佳答案

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
    });
}

关于node.js - Ember : The response to store. 查询应该是一个数组,但它是一条记录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42047196/

相关文章:

javascript - 使用 Browserify 导出 p5.js 函数

javascript - 获取Mac中所有可见应用程序窗口进程的窗口标题和应用程序/进程名称

node.js - 更改了 LESS 文件不会在 Node/express/coffee 设置中自动重新编译

javascript - 如何打破多个Ajax promise 链?

node.js - 内部/模块/cjs/loader.js :638 throw err; ^ Error: Cannot find module '../repositories/UserRepository' in docker container

node.js - 使用限制和排序时MongoDB解析错误

java - Parse.com 将 JSON 对象添加到 JSON 数组

ember.js - 为什么 "Assertion failed: You can only add a '故事“这段关系的记录”被抛出?

templates - 如何为登录页面定义一个模板/ View /任何内容,但为其他页面定义默认模板/ View /任何内容

mongodb - Mongo Go 驱动程序 Count() 方法从 Azure CosmosDB MongoDB Api Count() 获取 "Invalid response from server, value field is not a number"