This is a template you can use to report issues. You can also drag images, videos and include Preformatted text
Expected behavior
I would like to know if there is a way to validate data before creation? (mongodb)
Actual behavior
I am validating fields on my end, and intializing fields for backend computation (updatedAt, createdAt, etc)
// Create a Note
router.post('/notes', permissionMiddlewareCreator.create(), (request, response, next) => {
// Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#create-a-record
let forestAdminUser = request["user"] as IForestAdminUser;
const {query} = request;
const recordCreator = new RecordCreator<INote>(Notes, forestAdminUser, query);
let record = request.body.data.attributes as INote;
if (record.title === undefined || record.title === "") {
throw new Error(TITLE_NOT_SET_ERROR);
} else if (record.description === undefined || record.description === "") {
throw new Error(DESCRIPTION_NOT_SET_ERROR);
}
if (record[smartFieldToStr(SmartField.ProjectID)] !== undefined) {
if (record[smartFieldToStr(SmartField.ProjectID)] === "" || ProjectIDFromStr(record[smartFieldToStr(SmartField.ProjectID)]) === ProjectID.Undefined) {
throw new Error(PROJECT_ID_NOT_SET_ERROR);
} else {
record.project_id = ProjectIDFromStr(record[smartFieldToStr(SmartField.ProjectID)]);
}
} else {
throw new Error(PROJECT_ID_NOT_SET_ERROR);
}
record.created_at = getCurrentTimestampInSeconds();
record.image_url = DEFAULT_IMAGE_ON_CREATION;
record.is_active_in_prod = false;
record.updated_at = getCurrentTimestampInSeconds();
recordCreator.create(record as Record<string, any>).then(() => {
response.status(200).send("Note \""+record.title+"\" created.")
});
});
Failure Logs
I am getting this red banner: Note creation failed: unexpected error.
but the record does get created if the validation is passing, which I don’t really understand
Context
{
"name": "my-project",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./dist/server.js",
"build": "rm -rf ./dist && npm run compile",
"dev": "tsc-watch --onSuccess \"node ./dist/server.js\"",
"compile": "tsc",
"lint": "eslint . -c .eslintrc.json --ext .ts"
},
"dependencies": {
"@google-cloud/pubsub": "^2.17.0",
"@types/express": "^4.17.13",
"@types/mongoose": "^5.11.97",
"@types/node": "^16.3.3",
"abort-controller": "^3.0.0",
"body-parser": "^1.19.0",
"chalk": "~1.1.3",
"cookie-parser": "1.4.4",
"cors": "2.8.5",
"debug": "~4.0.1",
"dotenv": "~6.1.0",
"express": "~4.17.1",
"express-jwt": "6.0.0",
"forest-express-mongoose": "^8.3.0",
"mongoose": "~5.8.2",
"morgan": "1.9.1",
"node-fetch": "^2.6.2",
"nodemon": "^2.0.12",
"require-all": "^3.0.0",
"ts-node": "^10.1.0",
"tsc-watch": "^4.4.0",
"typescript": "^4.3.5"
}
}
Thanks
Max