Can not update the arrays from the put endpoint
This is one simple api using express where the results are stored in a course array. The validation logics are stored into the validateCourse function.
Now the problem is the POST
endpoint is working correctly but while I'm trying to update through the PUT
endpoint i'm getting 404 error:
name is required
.
Can anyone tell me where is the problem in the logic or the PUT endpoint thus the update option is not working.
const Joi = require('joi');
const express = require('express');
const app = express();
app.use(express.json());
const courses = [
{ id: 1, name: 'course1' },
{ id: 2, name: 'course2' },
{ id: 3, name: 'course3' }
]
app.post('/api/courses', (req, res) => {
const result = validateCourse(req.body);
// console.log(result);
if (result.error) {
res.status(400).send(result.error.details[0].message);
return;
}
const course = {
id: courses.length + 1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
app.put('/api/courses/:id', (req, res) => {
//Search course by get method
//If not found return error 404
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('not found with the given id');
//validate
//if invalid ,return 400 bad request
const result = validateCourse(req.body);
// const {error} = validateCourse(req.body);
if (result) {
res.status(400).send(result.error.details[0].message);
console.log(result.error.details);
return;
}
//if valid then update course
//Return the updated course to client
course.name = req.body.name;
res.send(course);
});
//INPUT VALIDATION//
function validateCourse(course) {
const schema = {
name: Joi.string().min(3).required()
};
return Joi.validate(course, schema);
}
javascript node.js express backend joi
|
show 5 more comments
This is one simple api using express where the results are stored in a course array. The validation logics are stored into the validateCourse function.
Now the problem is the POST
endpoint is working correctly but while I'm trying to update through the PUT
endpoint i'm getting 404 error:
name is required
.
Can anyone tell me where is the problem in the logic or the PUT endpoint thus the update option is not working.
const Joi = require('joi');
const express = require('express');
const app = express();
app.use(express.json());
const courses = [
{ id: 1, name: 'course1' },
{ id: 2, name: 'course2' },
{ id: 3, name: 'course3' }
]
app.post('/api/courses', (req, res) => {
const result = validateCourse(req.body);
// console.log(result);
if (result.error) {
res.status(400).send(result.error.details[0].message);
return;
}
const course = {
id: courses.length + 1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
app.put('/api/courses/:id', (req, res) => {
//Search course by get method
//If not found return error 404
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('not found with the given id');
//validate
//if invalid ,return 400 bad request
const result = validateCourse(req.body);
// const {error} = validateCourse(req.body);
if (result) {
res.status(400).send(result.error.details[0].message);
console.log(result.error.details);
return;
}
//if valid then update course
//Return the updated course to client
course.name = req.body.name;
res.send(course);
});
//INPUT VALIDATION//
function validateCourse(course) {
const schema = {
name: Joi.string().min(3).required()
};
return Joi.validate(course, schema);
}
javascript node.js express backend joi
1
Hi have you checked ifreq.body.name
is defined?
– t3__rry
Nov 21 '18 at 8:38
It's present in the course object as name:req.body.name. Do i have to define it through a new variable? @t3__rry
– MDIPANJAN
Nov 21 '18 at 8:48
I meant are you actually getting a value from thereq
object?
– t3__rry
Nov 21 '18 at 8:53
Just addconsole.log(req.body)
aboveconst result = validateCourse(req.body);
to see what data is comming in. Is the name there?
– Molda
Nov 21 '18 at 8:55
1
So the req.body is{}
if i understand, right? So there's no data coming in. How do you make the request to your app? From browser, Postman or something else??
– Molda
Nov 21 '18 at 9:08
|
show 5 more comments
This is one simple api using express where the results are stored in a course array. The validation logics are stored into the validateCourse function.
Now the problem is the POST
endpoint is working correctly but while I'm trying to update through the PUT
endpoint i'm getting 404 error:
name is required
.
Can anyone tell me where is the problem in the logic or the PUT endpoint thus the update option is not working.
const Joi = require('joi');
const express = require('express');
const app = express();
app.use(express.json());
const courses = [
{ id: 1, name: 'course1' },
{ id: 2, name: 'course2' },
{ id: 3, name: 'course3' }
]
app.post('/api/courses', (req, res) => {
const result = validateCourse(req.body);
// console.log(result);
if (result.error) {
res.status(400).send(result.error.details[0].message);
return;
}
const course = {
id: courses.length + 1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
app.put('/api/courses/:id', (req, res) => {
//Search course by get method
//If not found return error 404
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('not found with the given id');
//validate
//if invalid ,return 400 bad request
const result = validateCourse(req.body);
// const {error} = validateCourse(req.body);
if (result) {
res.status(400).send(result.error.details[0].message);
console.log(result.error.details);
return;
}
//if valid then update course
//Return the updated course to client
course.name = req.body.name;
res.send(course);
});
//INPUT VALIDATION//
function validateCourse(course) {
const schema = {
name: Joi.string().min(3).required()
};
return Joi.validate(course, schema);
}
javascript node.js express backend joi
This is one simple api using express where the results are stored in a course array. The validation logics are stored into the validateCourse function.
Now the problem is the POST
endpoint is working correctly but while I'm trying to update through the PUT
endpoint i'm getting 404 error:
name is required
.
Can anyone tell me where is the problem in the logic or the PUT endpoint thus the update option is not working.
const Joi = require('joi');
const express = require('express');
const app = express();
app.use(express.json());
const courses = [
{ id: 1, name: 'course1' },
{ id: 2, name: 'course2' },
{ id: 3, name: 'course3' }
]
app.post('/api/courses', (req, res) => {
const result = validateCourse(req.body);
// console.log(result);
if (result.error) {
res.status(400).send(result.error.details[0].message);
return;
}
const course = {
id: courses.length + 1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
app.put('/api/courses/:id', (req, res) => {
//Search course by get method
//If not found return error 404
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('not found with the given id');
//validate
//if invalid ,return 400 bad request
const result = validateCourse(req.body);
// const {error} = validateCourse(req.body);
if (result) {
res.status(400).send(result.error.details[0].message);
console.log(result.error.details);
return;
}
//if valid then update course
//Return the updated course to client
course.name = req.body.name;
res.send(course);
});
//INPUT VALIDATION//
function validateCourse(course) {
const schema = {
name: Joi.string().min(3).required()
};
return Joi.validate(course, schema);
}
javascript node.js express backend joi
javascript node.js express backend joi
edited Nov 21 '18 at 8:52
Molda
3,51511025
3,51511025
asked Nov 21 '18 at 8:34
MDIPANJANMDIPANJAN
186
186
1
Hi have you checked ifreq.body.name
is defined?
– t3__rry
Nov 21 '18 at 8:38
It's present in the course object as name:req.body.name. Do i have to define it through a new variable? @t3__rry
– MDIPANJAN
Nov 21 '18 at 8:48
I meant are you actually getting a value from thereq
object?
– t3__rry
Nov 21 '18 at 8:53
Just addconsole.log(req.body)
aboveconst result = validateCourse(req.body);
to see what data is comming in. Is the name there?
– Molda
Nov 21 '18 at 8:55
1
So the req.body is{}
if i understand, right? So there's no data coming in. How do you make the request to your app? From browser, Postman or something else??
– Molda
Nov 21 '18 at 9:08
|
show 5 more comments
1
Hi have you checked ifreq.body.name
is defined?
– t3__rry
Nov 21 '18 at 8:38
It's present in the course object as name:req.body.name. Do i have to define it through a new variable? @t3__rry
– MDIPANJAN
Nov 21 '18 at 8:48
I meant are you actually getting a value from thereq
object?
– t3__rry
Nov 21 '18 at 8:53
Just addconsole.log(req.body)
aboveconst result = validateCourse(req.body);
to see what data is comming in. Is the name there?
– Molda
Nov 21 '18 at 8:55
1
So the req.body is{}
if i understand, right? So there's no data coming in. How do you make the request to your app? From browser, Postman or something else??
– Molda
Nov 21 '18 at 9:08
1
1
Hi have you checked if
req.body.name
is defined?– t3__rry
Nov 21 '18 at 8:38
Hi have you checked if
req.body.name
is defined?– t3__rry
Nov 21 '18 at 8:38
It's present in the course object as name:req.body.name. Do i have to define it through a new variable? @t3__rry
– MDIPANJAN
Nov 21 '18 at 8:48
It's present in the course object as name:req.body.name. Do i have to define it through a new variable? @t3__rry
– MDIPANJAN
Nov 21 '18 at 8:48
I meant are you actually getting a value from the
req
object?– t3__rry
Nov 21 '18 at 8:53
I meant are you actually getting a value from the
req
object?– t3__rry
Nov 21 '18 at 8:53
Just add
console.log(req.body)
above const result = validateCourse(req.body);
to see what data is comming in. Is the name there?– Molda
Nov 21 '18 at 8:55
Just add
console.log(req.body)
above const result = validateCourse(req.body);
to see what data is comming in. Is the name there?– Molda
Nov 21 '18 at 8:55
1
1
So the req.body is
{}
if i understand, right? So there's no data coming in. How do you make the request to your app? From browser, Postman or something else??– Molda
Nov 21 '18 at 9:08
So the req.body is
{}
if i understand, right? So there's no data coming in. How do you make the request to your app? From browser, Postman or something else??– Molda
Nov 21 '18 at 9:08
|
show 5 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408026%2fcan-not-update-the-arrays-from-the-put-endpoint%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408026%2fcan-not-update-the-arrays-from-the-put-endpoint%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Hi have you checked if
req.body.name
is defined?– t3__rry
Nov 21 '18 at 8:38
It's present in the course object as name:req.body.name. Do i have to define it through a new variable? @t3__rry
– MDIPANJAN
Nov 21 '18 at 8:48
I meant are you actually getting a value from the
req
object?– t3__rry
Nov 21 '18 at 8:53
Just add
console.log(req.body)
aboveconst result = validateCourse(req.body);
to see what data is comming in. Is the name there?– Molda
Nov 21 '18 at 8:55
1
So the req.body is
{}
if i understand, right? So there's no data coming in. How do you make the request to your app? From browser, Postman or something else??– Molda
Nov 21 '18 at 9:08