Using graphql and nodejs, i am building an application that have product data as well as image. When I send a post request without image path, it works absolutely fine. But when I upload an image and attach it's path with product in mutation, an escape slash automatically added on the image path but the request raise an error Invalid character escape sequence: "\G"." with 400 graphql error
I am using angular 14
here is my product-store.component.ts
saveProduct() {
if(!this.form.valid){
return;
}
/***
* Save using graphQL service
* **/
this.productService.postSingleImage(this.form).subscribe((response)=>{
if(response.success === true){
this.product.image = response.filePath
// console.log(this.product.image)
this.graphqlService.createProduct(this.product).subscribe((response)=>{
if(response.createProduct._id){
this.toastr.success("Product Saved")
}
},error => { // Product Save Error
//let errorMessage = ErrorClass.getErrorMessage(error);
this.toastr.error(error.message);
console.error(error);
})
}
},error => {
this.toastr.error("Image Saving Failed");
console.error(error)
})
}
graphql.service.ts:
createProduct(product:Product){
const graphQlQuery = {
query: `
mutation{
createProduct(productInput:{
name: "${product.name}",
description: "${product.description}",
richDescription: "${product.richDescription}",
category: "${product.category}",
image: "${product.image}", // problem raised here
brand: "${product.brand}",
price: ${product.price},
countInStock: ${product.countInStock},
rating: ${product.rating},
numReviews: ${product.numReviews},
isFeatured: ${product.isFeatured},
})
{
_id
name
category{
name
}
description
richDescription
image
images
brand
price
countInStock
rating
numReviews
isFeatured
dateCreated
}
}
`
}
return this.http.post<any>(
graphApi,
{ query: graphQlQuery.query},
{headers:this.headers}) // header has only token information
.pipe(map((d) => d.data));
}
The error is :
{
"errors": [
{
"message": "Syntax Error: Invalid character escape sequence: \"\\G\".",
"locations": [
{
"line": 8,
"column": 21
}
]
}
]
}
when sending request, my payload image URL encoding with escape \ but it causing me to become fail to save my data using graphql.
** eg : "\public\uploads\images.jpg-1663390112242.jpeg"**
Can anyone help to find out what am I missing?
** I am using angular 14, node 16.13**