Setting up the JSON reply

When creating a schema using Joi and expecting a JSON response that matches the schema upon POSTing, an issue arises. The dilemma is having to include a parent element (in this case "data:") in the JSON response, although ideally the schema's attributes should stand alone without it. However, attempting to exclude the parent results in Object.assign(value) not functioning properly. Any suggestions on how to address this?

.post((req,res,next) => {
  let data = req.body;
  Joi.validate(data, schema, (err, value) => {

    res.json({
              data: Object.assign(value)
            });
  });

})

Expected Output:

{
    "title": "dasdawdasfasd",
    "textshort": "wasser",
    "textlong": "",
    "imgwidth": null,
    "imgheight": null,
    "imgsrc": "",
    "views": 0,
    "keywords": []
}

Actual Output:

{
  "data": {
    "title": "dasdawdasfasd",
    "textshort": "wasser",
    "textlong": "",
    "imgwidth": null,
    "imgheight": null,
    "imgsrc": "",
    "views": 0,
    "keywords": []
  }
}

Answer №1

Retrieve information

.post((request,response,next) => {
  Using Joi to check and validate the data against a schema
       Respond with JSON containing the validated value
  });

})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Vue.js is unable to find the reference for "_vm.KisanData" and throws a TypeError

I need assistance in fixing an error that I am encountering. The issue arises in this method where I am using KisanData, but I am unable to resolve it. const API_URL = "http://localhost:3000/User"; export default { name: "home", info(){ ...

What could be causing the malfunction in this JavaScript and WebRTC code?

<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Vid Chat App</title> </head> <body> <video controls autoplay> </video> <script src="https: ...

Enhancing search results with data retrieved from a jSON response

At the moment, I am using a logic to generate a list of titles. Now, I would like to include data from the response along with the code for each title in the list. const title = responseData.map(item => { return { label: item.title, val ...

Combining default and named exports in Rollup configuration

Currently, I am in the process of developing a Bluetooth library for Node.js which will be utilizing TypeScript and Rollup. My goal is to allow users to import components from my library in various ways. import Sblendid from "@sblendid/sblendid"; import S ...

Hot Module Replacement (HMR) for Redux Toolkit in React Native does not trigger updates in

TL;DR: Setting up the Redux Toolkit store in React Native for HMR correctly refreshes the app when changes are made, but the behavior of the reducer remains unchanged! Despite the announcement about React Native Reloading, it seems that the steps outlined ...

Inquiry regarding the management of data storage and retrieval for a sophisticated webpage

Currently, I am working with php and mysql within my stack. My current project involves designing a page that will showcase details of a mutual fund. The challenge lies in the fact that data for a single fund is spread across 15-20 different tables. To add ...

Can $.ajax be used as a replacement for $(document).ready(function()?

After conducting an extensive search, I am still unable to find a clear answer to my assumption. The code I used is as follows: <?php session_start(); if (isset($_SESSION['valid_user']) && $_SESSION['from']==1) { ?> ...

Session Cookie in Express Expires Prematurely

I am puzzled by how Express handles sessions. When I try setting the session to expire in 7 days like this: app.configure(function () { app.set(express.static(__dirname + '/public')); app.use(express.bodyParser()); app.use(express.cookiePa ...

What is the process by which headers are received in a pre-flight request without a response being generated?

Recently, I've delved into the world of CORS and pre-flight requests. From my understanding, it's essentially an initial OPTIONS request sent before the actual request is made. If the server approves, the real request follows. But what puzzles me ...

Creating dynamic menus in React JS involves mapping through an array of menu items and rendering

Click here to view the menu UI Currently facing an issue with loading menu and menu items dynamically from the backend using Redux state. Unable to display menu items for a specific menu. Data structure in JSON: menuContext:{ Define:[{id:1,label:" ...

Adding individual buttons at the bottom of each data row using Jquery: A step-by-step guide

Currently, I am receiving data from a backend using an AJAX GET method and displaying it in a list in HTML. However, I am facing some issues with including buttons within the list and making them functional by utilizing delegate and other methods. I would ...

Is it better to execute a function before using useState in React?

I am currently developing a random word generator using Next.js. The state of the three randomly generated words is being managed with the help of useState. However, I encountered an error message from React when I tried to implement it in the following ma ...

Is it possible to designate a column in mysql to automatically populate with the corresponding row number?

As a newcomer to project creation and SQL, I am encountering challenges with implementing a "rank" column that would display the ranking of a specific data row based on the "time" column. My project involves a full-stack CRUD database pulling from MySQL to ...

Experiencing issues with regex in JavaScript: all text enclosed within <angular> brackets vanishes

I am trying to analyze a formula and present it on the screen. For instance, I want to be able to input <path>T Q, where <path>T remains unchanged, and Q is a variable. It accepts this input, but when displaying it on the screen, only T Q shows ...

JavaScript: A timer that relies solely on the concept of TIME

Hello all, I have a specific question regarding starting a timer in JavaScript for a test scenario. Despite researching on my own and seeking help on various platforms, I haven't found a solution that fits my requirements. I am looking to implement a ...

displaying a Google Map within a designated container

I'm currently working on a basic website layout that consists of a full-width banner, a left menu (200px), and right content (600px). I have a simple jQuery script set up to load the content on the right-hand side when any of the five menu items are c ...

Modify the NAME attribute when clicked using Jquery

I am attempting to modify the NAME attribute of a DIV with the text from a textbox using jQuery. Take a look at my code snippet: http://jsfiddle.net/e6kCH/ Can anyone help me troubleshoot this issue? ...

The Azure Web App failed to start using NodeJS because the 'bcrypt' middleware was not functioning properly

After successfully setting up a new web app on Azure, I encountered an issue when trying to upload code from my Github. The website displayed the following error: The log shows: 2019-04-17T05:40:54.659490887Z Error: Cannot find module 'bcrypt&apos ...

Display the information stored in an array onto the console using console.log in Angular with Typescript

.html file <div class="container" *ngFor="let info of (this.info || [])"> <h5 class="card-title" (click)="getInfo()">{{info.paragraph1}}</h5> </div> .ts file getInfo () { *****console.lo ...

Storing events from FullCalendar into a database

I have been working on implementing the following markup: Within my project, I am using a fullcalendar instance. When a user clicks on a day, it triggers the dayClick callback function which opens a bootstrap modal. The user can then enter a title, start/ ...