in nestjs how to get dto list that include file? - nestjs

i want to get dtos that includes file in nestjs.
but i can't find how i get this json array in nestjs.
ex) json array
[
{brand : "A", file : file},
{brand : "B", file : file}
]
dto:
export class fileDto {
brand: string;
file: File;
}
i saw similar question.
File field is not being included in #Body decorator in NestJS
is there any way to get this array in nestjs ?

Related

Why does graphql not accept the parameters that I pass in the query?

I'm doing a practice with this library https://www.npmjs.com/package/json-graphql-server, I'm just trying to have the correct queries to be able to make a crud from the frontend but I don't understand how to add a new post since it does not accept it as a parameter of the query variables. apparently it does not recognize the post parameter that I am sending it, but on the right side of the documentation I have put in that variable all the fields that are required
mutation addPst($post: PostInput!){
createPost(post: $post) {
title
}
}
Query variables:
{
"post": {
"id": "100",
"title": "Curso de GraphQL",
"views": 0,
"user_id": 123
}
}
Example:
https://stackblitz.com/edit/json-graphql-server-nmctdj
Your mutation doesn't seem to be in line with the schema documentation you have posted.
The schema shows that the createPost mutation takes title, views and ID fields whereas you're passing in a "post" object.
Try rewriting the mutation as:
mutation addPst($title: String!, $views: Int!, $user_id: ID!){
createPost(title: $title, views: $views, user_id: $user_id) {
title
}
}

Syntax Error GraphQL Expected Name, found String

I am trying to implement mutations in GraphQL. I am using GraphQL Play Ground Ui for query.
Here is my mutation :
mutation{
createProduct (data :{
product: "abc",
product_status : {
"1" : {
order : "done"
}
}
Here is my TypeDef
type Product {
product : String,
product_status : JSON
}
But I am getting error in product_status excepted Name but found String as object contains 1 as string. How can i resolve this. I need to store this type of object in My database.Can someone help me to this.
Error Image
GraphQL supports strict data typing. So basically you can't have a string as a property key because that would mean it wouldn't know what to deserialize into. Also I'm not sure if JSON is a data type. I know strings and types but not JSON. You coudld have a object like:
type Product {
product : String,
product_status : ProductStatus
}
type ProductStatus {
id: ID,
order: String,
}
This is how you could design it.

Custom type for express.js Request interface

I am trying to change type of query in Express.js Request namespace. I allready using a custom atrribute, but this approach seems not working if attribute is already declared in #types (works just for new attributes added to Request).
This is my type declaration:
declare namespace Express {
export interface Query { [key: string]: any; }
export interface Request {
attr1?: string, // this is working
query: any // this is not working, query is still type of Query
}
}
and this is my ts.config
"typeRoots" : [
"./src/types",
"node_modules/#types"
]
Is there some another approach, how to redefine type of query attribute? (or another attribute from Request interface)
It seems only solution is use an Omit
import { Request as OriginalRequest } from 'express'
declare namespace Express {
export interface Request extends Omit<OriginalRequest, 'query'> {
query: any
}
}

How to pass ObjectID as query parameter in node-red to mongodb?

I am using node-red-contrib-mongodb3 and having trouble to pass an ObjectId as an argument, not being able to use a MongoDB auto-generated _id, is there any way to use "_id": 'ObjectId("myID")' as an argument?
I have followed the module tutorial on passing query parameters node-red-contrib
Here is an example:
I have the following document in my collection:
{
"_id" : ObjectId("5c9156c7f8c3ec3259454571"),
"name" : "teste_site_1",
}
If I pass as msg.payload to findOne Operation
msg.payload = { "name" : "teste_site_1" }
returns my document without ObjectID in _id
{
"_id" : "5c9156c7f8c3ec3259454571",
"name" : "teste_site_1",
}
if I pass the _id as argument:
msg.payload = { "_id" : "5c9156c7f8c3ec3259454571" }
returns empty.
I can not call ObjectId in node-red and also cannot pass as string ObjectId.
I wonder if there is already a way to pass ObjectId as an argument.
Is this a bug or am I missing something?
I have solved by using as a parameter the return of ObjectId function in 'mongodb' node module but in order to use node modules inside a function node, one must import it to global context inside your node-red directory.
on windows:
C:\\users\username\.node-red\settings.js
in Ubuntu
~\.node-red\settings.js
and include the ObjectId as global import
functionGlobalContext: {
require: require, // Not mandatory
ObjectId : require('mongodb').ObjectID,
},
I have also included 'require' module for some may find it useful to not mess with settings.js and also make it unnecessary the process restart to add future modules.
And inside the function node, I just passed the payload as follows.
var ObjectId = global.get('ObjectId');
msg.payload={
"_id":ObjectId("5c9156c7f8c3ec3259454571")
};
return msg;

How to edit a .js file with nodes

I'm trying to figure out if it's possible to simply open up a JSON data script with nodejs and edit it... as javascript ...
// my json data
{
people : [{
name : 'Joe',
hobby : 'hunting',
job : 'accountant'
},{
name : 'William',
hobby : 'chess',
job : 'manager'
}]
}
i just want to do something like e.g.
people[0].name = 'Joseph'
so i'm trying
fs.open('/path/to/file', 'r+', function(err, fd){
// not really sure what to do from here...
})
there are plenty of answers about how to read/write text files... i just thought there might be an easier way for the case of a file in JSON
Whoops this is a repeat question - please see an earlier response here
How to update a value in a json file and save it through node.js
Parse the JSON using JSON.parse(), modify the parsed object, turn it back into a new string using JSON.stringify(), then save the string to the file.

Resources