I know i18n-abide add a req.gettext but how to handle strings outside a route/controller?
I mean for example, if I have strings to be translated in a mongoose schema file how can I use the gettext method?
Related
Let's say we make the following GET API call to our REST API running with NestJS (and fastify):
http://localhost:5000/api/test?arrayParam=["abc","def"]&anotherParam="value"
Without parsing anything, the query object on the backend looks like this:
{
arrayParam: '["abc","def"]',
anotherParam: '"value"'
}
We can see that parameter values are strings, but in the case of arrayParam we would obviously like to work with the actual array.
I come from an expressJS background, and coming from there, there are a couple of approaches. First would be just using a JSON parser middleware, like body-parser. Or just using JSON.parse().
But what is the "proper", NestJS approach? I thought about using type decorators defined in a DTO, and assumed they would be automatically parsed to the type that I defined. But that doesn't work like I assumed it would.
I defined it like this:
#IsOptional()
#IsArray()
arrayParam?: string[];
But validation fails, since arrayParam is a string and not an array. So I assume this is not the correct approach
You are sending it incorrectly
http://localhost:5000/api/test?arrayParam[]=abc&arrayParam[]=def&anotherParam
So I have a Nested Many Schema (eg Users) inside another Schema (eg Computer). My input object to be deserialised by the Schema is complex and does not allow for assignment, and to modify it to allow for assignment is impractical.
The input object (eg ComputerObject) itself does not contain an a value called "Users", but nested in a few other objects is a function that can get the users (eg ComputerObject.OS.Accounts.getUsers()), and I want the output of that function to be used as the value for that field in the schema.
Two possible solutions exist that I know of, I could either define the field as field.Method(#call the function here) or I could do a #post_dump function to call the function and add it to the final output JSON as it can provide both the initial object and the output JSON.
The issue with both of these is that it then doesn't serialise it through the nested Schema Users, which contains more nested Schemas and so on, it would just set that field to be equal to the return value of getUsers, which I don't want.
I have tried to define it in a pre-dump so that it can then be serialised in the dump (note: this schema is used only for dumping and not for loading), but as that takes in the initial object I cannot assign to it.
Basically, I have a thing I am trying to do, and a bunch of hacky workarounds that could make it work but not without breaking other things or missing out on the validation altogether, but no actual solution it seems, anybody know how to do this properly?
For further info, the object that is being input is a complex Django Model, which might give me some avenues Im not aware of, my Django experience is somewhat lacking.
So figured this out myself eventually:
Instead of managing the data-getting in the main schema, you can define the method used in the sub-schema using post_dump with many=True, thus the following code would work correctly:
class User(Schema):
id = fields.UUID
#pre_dump(pass_many=True)
def get_data(self, data, **kwargs):
data = data.Accounts.getUsers()
return data
class Computer(Schema):
#The field will need to be called "OS" in order to correctly look in the "OS" attribute for further data
OS = fields.Nested(User, many=True, data_key="users")
When using Express normally you use app.post('/path') or app.get('/path') to define routes.
Does Express also provide a function to define routes like this app.route('POST', '/path') (where route is the function I'm looking for)?
You can programmatically select method with bracket notation. For example:
app['post']('/path')
app['post']('/path-2')
app['get']('/path')
I am setting-up the server response of a query to couchbase and want to use handlerbars to render the response data.
I understand that the best practice is to have my helper functions into a separate file and not be embedded in a script tag in my html file.
My question is what is the best practice or technique to pass the data from the server response to my hanldbars helper file to then be manipulated?
I am using hapijs on the server and jQuery on the client.
Well I might be wrong, but following this example I found, it seems like you export the helper file like you would any other module with the module.exports
http://codyrushing.com/using-handlebars-helpers-on-both-client-and-server/
According to the API documentation for hapi the helper file must export a single method with the signature `function(context).
Helpers are functions used within templates to perform transformations
and other data manipulations using the template context or other
inputs. Each '.js' file in the helpers directory is loaded and the
file name is used as the helper name. The files must export a single
method with the signature function(context) and return a string.
Sub-folders are not supported and are ignored. Defaults to no helpers
support (empty path). Note that jade does not support loading helpers
this way.
https://github.com/hapijs/hapi/blob/master/docs/Reference.md#route-options
Is there a way to tell MongoClient that there are some properties I don't want to store? For example, I have some properties that are cyclic dependencies which fail to serialize - this causes a few problems. I'd rather not have to set them to null before I save them every time, then re-instate those variables when the insert has finished.
One way to do this is with a little help from the omit method of the underscore (or lodash) library. That will cleanly create a copy of your object without the problematic properties.
var objectToInsert = _.omit(fullObject, 'badField1', 'badField2');
collection.insert(objectToInsert, callback);
Another way to go is to use Mongoose which lets you define schemas for your collections so that only those fields in the schema get included when saved.