TypeError:对象[object Object],[object Object]找不到方法

雅各布

我已经在《 Sitepoint Full Stack Javascript with MEAN》一书中跟随了一个教程,我刚刚完成了第6章,并且应该已经创建了一个带有“数据库”的“服务器”。该数据库不过是一个JSON文档。但是,尽管(我所看到的),我的代码是他的直接副本,但是当我尝试运行它时,却遇到了标题中提到的错误。这是var result = data.find(function(item) {...(位于employees.js线,约16行),它是此问题的原因。我看不到我还能做些什么,希望大家能找到解决我问题的办法。

我有几个用于此目的的不同文件。

Index.js:

var http = require('http');
var employeeService = require('./lib/employees');
var responder = require('./lib/responseGenerator');
var staticFile = responder.staticFile('/public');

http.createServer(function(req,res) {
    // a parsed url to work with in case there are parameters
    var _url;

    //In case the client uses lower case for methods
    req.method = req.method.toUpperCase();
    console.log(req.method + ' ' + req.url);

    if (req.method !== 'GET') {
        res.writeHead(501, {
            'Content-Type': 'text/plain'
        });
        return res.end(req.method + ' is not implemented by this server.');
    }

    if (_url = /^\/employees$/i.exec(req.url)) {
        //return a list of employess
        employeeService.getEmployees(function(error, data){
            if(error) {
                return responder.send500(error, res);
            }
            return responder.sendJson(data,res);
        });
    } else if (_url = /^\/employees\/(\d+)$/i.exec(req.url)){ 
        //find the employee by the id in the route
        employeeService.getEmployee(_url[1], function(error, data) {
            if (error) {
                return responder.send500(error, res);
            }
            if(!data) {
                return responder.send404(res);
            }
            return responder.sendJson(data,res);
        });

    } else{
            res.writeHead(200);
            res.end("static file")
    }


}).listen(1337);

console.log('server running');

employee.js

var employeeDb = require('../database/employees.json')

exports.getEmployees = getEmployees;
exports.getEmployee = getEmployee;

function getEmployees (callback) {
    setTimeout(function() {
        callback(null, employeeDb);
    }, 500);
}

function getEmployee (employeeId, callback) {
    getEmployees(function (error, data) {
        if (error) {
            return callback(error);
        }
        var result = data.find(function(item) {
            return item.id === employeeId;
        });
        callback(null, result)
    });
}

responseGenerator.js

var fs = require('fs');

exports.send404 = function (reponse) {
    console.error('Resource not found');
    response.writeHead(404, {
        'Content-Type': 'text/plain'
    });
    response.end('Not Found');
}

exports.sendJson = function(data, response) {
    response.writeHead(200, {
        'Content-Type': 'application/json'
    });

    response.end(JSON.stringify(data));
}

exports.send500 = function(data, response) {
    console.error(data.red);
    reponse.writeHead(500, {
        'Content-Type': 'text/plain'
    });
    response.end(data);
}

exports.staticFile = function(staticPath) {
    return function(data, response) {
        var readStream;

        // Fix so routes to /home and /home.html both work
        data = data.replace(/^(\/home)(.html)?$/i,'$1.html');
        data = '.' + staticPath + data;

        fs.stat(data, function(error, stats) {
            if (error || stats.isDirectory()) {
                return exports.send404(response);
            }

            readstream = fs.createReadStream(data);
            return readStream.pipe(response);
        });
    }
}

employee.json(“数据库”)

[
    {
        "id": "103",
        "name": {
            "first": "Colin",
            "last": "Ihrig"
        },
        "address": {
            "lines": ["11 Wall Street"],
            "city": "New York",
            "state": "NY",
            "zip": 10118
        }
    },
    {
        "id": "104",
        "name": {
            "first": "Idiot",
            "last": "Fjols"
        },
        "address": {
            "lines": ["Total taber"],
            "city": "Feeeee",
            "state": "Whatever",
            "zip": 10112
        }
    }


]

希望能对您有所帮助。

维沙尔帕特尔

您可以尝试使用.filter方法而不是.find方法。或者将数据库中的数组更改为json。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

找不到不同的支持对象“[object Object]”

来自分类Dev

TypeError:对象#<Object>没有方法

来自分类Dev

找不到类型为“object”的不同支持对象“[object Object]”,流星集合中出错

来自分类Dev

找不到类型为“object”的不同支持对象“[object Object]”。NgFor 仅支持

来自分类Dev

JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

来自分类Dev

JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

来自分类Dev

TypeError:对象[object Object]的属性xxx不是函数

来自分类Dev

找不到javascript实例方法:对象#<Object>没有方法'handleRequest'

来自分类Dev

TypeError:undefined不是对象(评估'object ['body']')

来自分类Dev

CMAKE:包含用OBJECT找不到的文件

来自分类Dev

找不到json_object_iterator.h

来自分类Dev

在Angular 8中找不到其他支持对象'[object Object]

来自分类Dev

“找不到'object'类型的不同支持对象'[object Object]'。NgFor仅支持绑定到Iterables,例如数组。”

来自分类Dev

Angular 5:'找不到'object'类型的不同支持对象'[object Object]'。NgFor 仅支持绑定到 Iterables,例如 Arrays'

来自分类Dev

Angular 找不到类型为“object”的不同支持对象“[object Object]”。NgFor 只支持绑定到 Iterables,比如 Arrays

来自分类Dev

找不到类型为“object”的不同支持对象“[object Object]”。NgFor 只支持绑定到 Iterables 之类的 Arrays 错误

来自分类Dev

JavaScript Uncaught TypeError:对象[object global]没有方法

来自分类Dev

未捕获的TypeError:对象#<Object>没有方法'vibrate'

来自分类Dev

流星:TypeError:对象#<Object>没有方法'require'

来自分类Dev

未捕获的TypeError:对象#<Object>没有方法'closest'

来自分类Dev

节点js:TypeError:对象#<Object>没有方法'existsSync'

来自分类Dev

TypeError:对象#<Object>在节点中没有方法'Router'

来自分类Dev

未捕获的TypeError:对象#<Object>没有方法'Sin'

来自分类Dev

AngularJS:TypeError:对象#<Object>没有方法'replace'

来自分类Dev

TypeError:对象#<Object>没有方法'Schema'

来自分类Dev

TypeError:对象#<Object>没有方法'save'

来自分类Dev

未捕获的TypeError:对象[object Object]没有方法'select2'

来自分类Dev

Node.js => TypeError:对象[object Object]没有方法'save'

来自分类Dev

使用$()。trigger()并获得TypeError:对象[object Object]没有方法'apply'

Related 相关文章

  1. 1

    找不到不同的支持对象“[object Object]”

  2. 2

    TypeError:对象#<Object>没有方法

  3. 3

    找不到类型为“object”的不同支持对象“[object Object]”,流星集合中出错

  4. 4

    找不到类型为“object”的不同支持对象“[object Object]”。NgFor 仅支持

  5. 5

    JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

  6. 6

    JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

  7. 7

    TypeError:对象[object Object]的属性xxx不是函数

  8. 8

    找不到javascript实例方法:对象#<Object>没有方法'handleRequest'

  9. 9

    TypeError:undefined不是对象(评估'object ['body']')

  10. 10

    CMAKE:包含用OBJECT找不到的文件

  11. 11

    找不到json_object_iterator.h

  12. 12

    在Angular 8中找不到其他支持对象'[object Object]

  13. 13

    “找不到'object'类型的不同支持对象'[object Object]'。NgFor仅支持绑定到Iterables,例如数组。”

  14. 14

    Angular 5:'找不到'object'类型的不同支持对象'[object Object]'。NgFor 仅支持绑定到 Iterables,例如 Arrays'

  15. 15

    Angular 找不到类型为“object”的不同支持对象“[object Object]”。NgFor 只支持绑定到 Iterables,比如 Arrays

  16. 16

    找不到类型为“object”的不同支持对象“[object Object]”。NgFor 只支持绑定到 Iterables 之类的 Arrays 错误

  17. 17

    JavaScript Uncaught TypeError:对象[object global]没有方法

  18. 18

    未捕获的TypeError:对象#<Object>没有方法'vibrate'

  19. 19

    流星:TypeError:对象#<Object>没有方法'require'

  20. 20

    未捕获的TypeError:对象#<Object>没有方法'closest'

  21. 21

    节点js:TypeError:对象#<Object>没有方法'existsSync'

  22. 22

    TypeError:对象#<Object>在节点中没有方法'Router'

  23. 23

    未捕获的TypeError:对象#<Object>没有方法'Sin'

  24. 24

    AngularJS:TypeError:对象#<Object>没有方法'replace'

  25. 25

    TypeError:对象#<Object>没有方法'Schema'

  26. 26

    TypeError:对象#<Object>没有方法'save'

  27. 27

    未捕获的TypeError:对象[object Object]没有方法'select2'

  28. 28

    Node.js => TypeError:对象[object Object]没有方法'save'

  29. 29

    使用$()。trigger()并获得TypeError:对象[object Object]没有方法'apply'

热门标签

归档