I am new to NodeJs, I created several APIs for my application with different methods and they all work fine, the issue is that I created a new API in the same file where I have the others but when calling it from my FE it is called twice, one is prefligth with options method and another is pending but without method... Attached screenshots..API Call
This is my API code:
router.put("/carrousel-item/update/:id", fileUpload, (req, res) => {
req.getConnection((err, conn) => {
try {
const image = fs.readFileSync(
path.join(__dirname, "../images/" req.file.filename)
);
const title_en = req.body.title_en;
const title_es = req.body.title_es;
const sub_title_color_1_en = req.body.sub_title_color_1_en;
const sub_title_color_1_es = req.body.sub_title_color_1_es;
const sub_title_color_2_en = req.body.sub_title_color_2_en;
const sub_title_color_2_es = req.body.sub_title_color_2_es;
try {
conn.query(
`UPDATE home_team_section SET ? WHERE id = ?`,
[
{
title_en: title_en,
title_es: title_es,
sub_title_color_1_en: sub_title_color_1_en,
sub_title_color_1_es: sub_title_color_1_es,
sub_title_color_2_en: sub_title_color_2_en,
sub_title_color_2_es: sub_title_color_2_es,
image: image,
},
],
(err, rows) => {
res.send(rows);
}
);
} catch (error) {
console.log(error);
}
} catch (error) {}
});
});
This problem is not letting me advance with the project since it does not let me execute any new API
Note: I have added the cors()
I hope you can help me
CodePudding user response:
const ModelLabel = require("../models/modelLabel")
const express=require('express')
const router=express.Router()
class ControllerLable{
static label(request,response){
ModelLabel.renderLabel((err,data)=>{
if (err) {
response.send(err)
}else{
// console.log(data);
response.render('labelView',{ newResult:data })
}
})
}
CodePudding user response:
static addSongGet(cb){
let query=`
SELECT l."name",l.id FROM "Labels" l;`
pool.query(query,(err, res)=>{
if (err) {
cb(err)
}else{
let newResult=res.rows
cb(null,newResult)
}
})}
static addSongPost(request,cb){
const title1=request.body.title
const bandName1=request.body.bandName
const duration1=request.body.duration
const genre1=request.body.genre
const lyric1=request.body.lyric
const imageUrl1=request.body.imageUrl
const label1=request.body.label
const createdAt1=request.body.createdAt
let error=[]
if (!title1) {
error.push("Title is required")
}
if (title1.length>=100) {
error.push("Title maximum character is 100")
}
if (!bandName1) {
error.push("BandName is required")
}
if (!duration1) {
error.push("Duration is required")
}
if (duration1<60) {
error.push("Minimum Duration is 60 second")
}
if (!genre1) {
error.push("Genre is required")
}
if (!lyric1) {
error.push("Lyric is required")
}
if (lyric1) {
let countSpace=0
for (let i = 0; i < lyric1.length; i ) {
if (lyric1[i]===" ") {
countSpace
}
}
// console.log(countSpace);
if (countSpace<10) {
error.push("Minimum word in lyric is 10")
}
}
if (!imageUrl1) {
error.push("Image Url is required")
}
if (imageUrl1<=50) {
error.push("ImageUrl name maximum character is 50")
}
if (!label1) {
error.push("Label Company is required")
}
if (!createdAt1) {
error.push("Date is required")
}
if (createdAt1) {
let currentDate = new Date().toJSON().slice(0, 10);
if (createdAt1>currentDate) {
error.push("Maximum created date is today")
}
}
// console.log(error);
if (error.length!==0) {
// console.log(error);
cb(error)
}else{
let vote;
const {title,bandName,duration,genre,lyric,imageUrl,label,createdAt}=request.body
const values=[title,bandName,duration,genre,createdAt,lyric,imageUrl,vote=0,label]
let query=`
INSERT INTO "Songs" ("title","bandName","duration","genre","createdDate",lyric,"imageUrl","totalVote","LabelId")
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9);`
pool.query(query,values,(err,res)=>{
if (err) {
cb(err)
console.log('QUERY EROR');
}else{
console.log("Data berhasil ditambahkan");
cb(null,1)
}
})
}
}