Anonymous Message Board /api/threads/{board} fail

Hi i don’t know what wrong.

can you help me pls?

1 Like

What’s the problem? Can you tell me? I don’t know about this but I’m trying to help you.

1 Like

Thank you for Reply

My problem is that I can’t find it.
Everything is normal, everything can be connected.
Run all tests through. But I’m stuck on one thing that I don’t know what it is.

This is the part that is relevant to the problem.

`

.put((req, res) => {
Message.findOne({ board: req.params.board, _id: mongoose.Types.ObjectId(req.body.thread_id) }, async (err, doc) => {
if (err) {
throw err;
}
if (!doc) {
res.send(“invalid thread ID”);
} else {
const result = doc.replies.findIndex(({ _id }) => {
return JSON.stringify(_id) === "${req.body.reply_id}";
});
if (isNaN(result)) {
res.send(‘invalid reply ID’);
} else {
doc.replies[result].reported = true;
await doc.save();
res.send(‘reported’);
}
}
});
})

`

The line return JSON.stringify(_id) === "${req.body.reply_id}"; should use backticks for string interpolation instead of double quotes

return JSON.stringify(_id) === `${req.body.reply_id}`;
1 Like

this’s more detail error

// running tests
You can send a PUT request to /api/threads/{board} and pass along the thread_id. Returned will be the string reported. The reported value of the thread_id will be changed to true.
// tests completed
// console output
[Error: expected ‘invalid id’ to equal ‘reported’]

1 Like

Thank you I tried it and it didn’t work.

1 Like

You are sending invalid id in the /api/threads/:board PUT handler. Are you sure the body payload contains a report_id property?

Your PUT handler for the /api/replies/:board endpoint is crashing. If you do not see it add --watch to the start script or add a try/catch to the code and log the error. I believe your findIndex is the cause, isNaN does not prevent you from using -1 for the index for doc.replies

1 Like

app.route(‘/api/threads/:board’)
.put((req, res) => {
Message.findOneAndUpdate({ board: req.params.board, _id: mongoose.Types.ObjectId(req.body.thread_id) }, { $set: { reported: true } }, { useFindAndModify: false }, (err, doc) => {
if (err) {
throw err;
}
if (!doc) {
res.send(‘invalid id’);
} else {
res.send(‘reported’);
}
});
})

This code adds a route handler for PUT requests to /api/threads/:board , which retrieves the board parameter from the request URL and the thread_id parameter from the request body. The code then uses the findOneAndUpdate() method to find the relevant Message document and update its reported field to true . If the operation is successful, the server sends a response of “reported”, otherwise it sends a response of “invalid id”.

done i use this from GPT LOL Thank everyone

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.