Я пытаюсь проверить, правильно ли вводит мое приложение e js.
Я думаю, что получаю ошибку, потому что в функции маршрутизатора есть пара вложенных функций, которые я пытаюсь проверить.
main. js:
'use strict';
const express = require('express');
const app = express();
const homeController = require('./controllers/homeController');
const errorController = require('./controllers/errorController');
const path = require('path');
const layouts = require("express-ejs-layouts");
console.log("+get homeController");
app.get('/', homeController);
console.log('+post homeController.addUsers');
app.post('/users/submit', homeController.addUsers);
...
module.exports = app
homeController. js
const express = require('express');
const router = express.Router();
...
router.addUsers = (req, res) => {
console.log("in homeController addUser");
var newUserName = req.body.name;
console.log("name " + newUserName);
var newUsergrade = req.body.grade;
var temp = 0;
newUsergrade.forEach(letter => {
letter = letter.toUpperCase();
//giant switch case for converting letters
}
var valid = false;
if (temp / 4 >= 2.5) {
valid = true;
} else {
vaild = false;
}
col.insertOne({
name: newUserName,
grade: newUsergrade,
isValid: valid
}, function(err, r) {
test.equal(null, err);
test.equal(1, r.insertedCount);
col.find({}).toArray((err, userData) => {
console.log("record found: ", userData);
usersArray = userData;
});
});
usersArray.push({
name: newUserName
});
res.render("users", {
allUsers: usersArray,
title: "Users List"
});
};
...
router.test. js
var sinon = require('sinon');
const chai = require("chai"),
{
expect
} = chai,
router = require("./controllers/homeController.js"),
chaiHTTP = require("chai-http"),
app = require("./main.js");
chai.use(chaiHTTP);
describe("addUsers", () => {
it("should make sure a user can be added to the array", () => {
var usersArray = {
name: "blake",
grade: ["a", "a", "b", "d"]
};
expect(router.addUsers(usersArray)).to.deep.include({
name: "blake",
grade: ["a", "a", "b", "d"]
});
});
it("should return an empty object with empty request body input", () => {
var emptyBody = {};
expect(router.addUsers(emptyBody)).to.deep.include({});
});
});
ошибка
1) addUsers
should make sure a user can be added to the array:
TypeError: Cannot read property 'name' of undefined
Я получаю эту ошибку, потому что router.addUsers
имеет функцию req, res arrow или res.render?