0

在我尝试运行的一个简单示例中,我遇到了一些关于 jQuery 的菜鸟问题。firefox工具给出了一个Syntax Error: JSON.parse: unexpected end of data,我真的找不到调试它的方法。

我已经用下面的数据填充了我的 mongodb,它们看起来确实经过验证。

var employees = [
    {"id": 1, "firstName": "James", "lastName": "King", "managerId": 0},
    {"id": 2, "firstName": "Julie", "lastName": "Taylor", "managerId": 1},
    {"id": 3, "firstName": "Eugene", "lastName": "Lee", "managerId": 1},
    {"id": 4, "firstName": "John", "lastName": "Williams", "managerId": 1}
];

我正在尝试将其退回

exports.findAll = function(req, res) {
var name = req.query["name"];
    db.collection("employees", function(err, collection) {
        if (name) {
            collection.find({"fullName": new RegExp(name, "i")}).toArray(function(err, items) {
                console.log(items);
                res.jsonp(items);
            });
        } else {
            collection.find().toArray(function(err, items) {
                console.log(items);
                res.jsonp(items);
            });
        }
    });
};

并在浏览器上阅读

$.get("http://localhost:8888/employees",function(jsonp){
    //document.write("json");       
});

编辑:在浏览器上使用 url 返回有效数据,但是,对于相同的操作,console.log 返回无效数据。这就是让我怀疑双重解析问题的原因,如果有类似的话。

browser-> { "id": 4, "firstName": "John", "lastName": "Williams", "managerId": 1, "_id": 5276247a3f2e4cf040000004 }
console-> { id: 4, firstName: 'John', lastName: 'Williams', managerId: 1, _id: 5276247a3f2e4cf040000004 }
4

0 回答 0