I created a function to check if in my database a user exists according to the sub
parameter. I would like my function to give me a true
or false
value.
Front-end :
......
async function exist(sub) {
const result = await InfoDataService.get(sub)
alert(result.sub);
}
......
Controller :
// Find a User with sub
exports.findOne = (req, res) => {
const sub = req.params.sub;
User.findOne({sub})
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message: "Error retrieving User with id=" +sub
});
});
};
Route :
module.exports = app => {
const users = require("../controllers/user.controller.js");
const router = require("express").Router();
// Retrieve
router.get("/:sub", users.findOne);
app.use('/api/users', router);
};
InfoDataService :
import http from "../http-common.js";
const get = sub => {
return http.get(`/users/${sub}`);
};
export default {
get,
};
http-common :
import axios from "axios";
export default axios.create({
method: 'GET',
baseURL: "http://localhost:8080/api",
});
If instead of result.sub
I use only result
I can retrieve the object, but if I try to access the .sub
property I get "undefined".
I noticed another thing that is probably related: the path for post is api/users
, while for get it is api/users/:sub
. Checking the network I found that the asynchronous function makes the request to api/users/undefined
. Where would the error be?
Not sure if User.findOne({sub})
is correct, should maybe rather be a where clause there? { where: sub}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.