Я пытаюсь создать веб-приложение, и у меня действительно возникают проблемы с подключением к базе данных.
Но теперь я должен сделать доступ к базе данных.По этой причине я использую Postman для проверки своих POST- и GET-операторов.
ReferenceError: Tools is not defined
<br> at list (server\controllers\tools.js:17:5)
<br> at Layer.handle [as handle_request] (server\node_modules\express\lib\router\layer.js:95:5)
<br> at next (server\node_modules\express\lib\router\route.js:137:13)
<br> at Route.dispatch (server\node_modules\express\lib\router\route.js:112:3)</p>
</blockquote>
<p>And I really don't get why it always says "Tools is not defined".
I have a database scheme called "public".
So maybe that could be a thing?
I also tried to set database to public.[DATABASENAME] but it doesn't changed a thing. </p>
<p>I hope you guys can help me and I described the case good enough.</p>
<p>My files for that looks like that:</p>
<p><strong>/server/config/config.json</strong></p>
<pre><code>{
"development": {
"username": "[USERNAME]",
"password": "[PASSWORD]",
"database": "testdb",
"host": "localhost",
"port": [PORT],
"dialect": "postgres"
}
</code>
/ route / index.js
const toolsController = require('../controllers').tools;
const toolitemsController = require('../controllers').toolitems;
module.exports = (app) => {
app.get('/api', (req, res) => res.status(200).send({
message: 'Welcome to the tools API!',
}));
app.post('/api/tools', toolsController.create);
app.get('/api/tools', toolsController.list);
app.get('/api/tools/:toolId', toolsController.retrieve);
app.put('/api/tools/:toolId', toolsController.update);
app.delete('/api/tools/:toolId', toolsController.destroy);
app.post('/api/tools/:toolId/items', toolitemsController.create);
app.put('/api/tools/:toolId/items/:toolitemId', toolitemsController.update);
app.delete(
'/api/tools/:toolId/items/:toolitemId', toolitemsController.destroy
);
app.all('/api/tools/:toolId/items', (req, res) => res.status(405).send({
message: 'Method Not Allowed',
}));
};
/ server / controllers / tools.js
const tool = require('../models').tool;
const toolitem = require('../models').toolitem;
module.exports = {
create(req, res) {
return Tools
.create({
tool_id: req.body.tool_id,
tool_name: req.body.tool_name,
status: req.body.status
})
.then((tools) => res.status(201).send(tools))
.catch((error) => res.status(400).send(error));
},
list(req, res) {
return Tools
.all()
.then(tools => res.status(200).send(tools))
.catch(error => res.status(400).send(error));
},
};
отредактированная версия / server / controllers / tools.js
const tools = require('../models').tools;
const toolitem = require('../models').toolitem;
module.exports = {
create(req, res) {
return tools
.create({
tool_id: req.body.tool_id,
tool_name: req.body.tool_name,
status: req.body.status
})
.then((tools) => res.status(201).send(tools))
.catch((error) => res.status(400).send(error));
},
list(req, res) {
return tools
.all()
.then(tools => res.status(200).send(tools))
.catch(error => res.status(400).send(error));
},
/*
list(req, res) {
return tool
.findAll({
include: [{
model: toolitem,
as: 'toolitems',
}],
order: [
['createdAt', 'DESC'],
[{ model: toolitem, as: 'toolitems' }, 'createdAt', 'ASC'],
],
})
.then((tools) => res.status(200).send(tools))
.catch((error) => res.status(400).send(error));
},*/
retrieve(req, res) {
return tools
.findById(req.params.toolId, {
include: [{
model: toolitem,
as: 'toolitems',
}],
})
.then((tools) => {
if (!tools) {
return res.status(404).send({
message: 'tools Not Found',
});
}
return res.status(200).send(tools);
})
.catch((error) => res.status(400).send(error));
},
update(req, res) {
return tools
.findById(req.params.toolId, {
include: [{
model: toolitem,
as: 'toolitems',
}],
})
.then(tools => {
if (!tools) {
return res.status(404).send({
message: 'tools Not Found',
});
}
return tools
.update({
title: req.body.title || tool.title,
})
.then(() => res.status(200).send(tools))
.catch((error) => res.status(400).send(error));
})
.catch((error) => res.status(400).send(error));
},
destroy(req, res) {
return tools
.findById(req.params.toolId)
.then(tools => {
if (!tools) {
return res.status(400).send({
message: 'tool Not Found',
});
}
return tools
.destroy()
.then(() => res.status(204).send())
.catch((error) => res.status(400).send(error));
})
.catch((error) => res.status(400).send(error));
},
};