我想创建可以在传入数据到达数据库之前对其进行转换的功能。假设我们要确保在创建新用户时, firstName 和 lastName 属性值始终以大写字母开头。或者另一个很好的例子是防止登录问题并始终以小写字母存储电子邮件地址。
我查看了 NestJS 的管道文档,但它太通用了。我希望能够指定哪些字段需要转换,并且我不想为每个需要转换的属性或端点创建管道。
我也尝试过 @Transform 装饰器形成“class-transformer”包,但这似乎不起作用。
export class UserRoleCreateDto {
@IsDefined()
@IsString()
@IsNotEmpty()
@Transform((name) => name.toUpperCase())
readonly name;
}
Run Code Online (Sandbox Code Playgroud)
预期的结果应该是大写字母的名称,但事实并非如此。
任何关于如何在 NestJS / TypeORM 命中数据库之前实现正确输入转换的想法或示例?
谢谢你的时间!
我很难找到任何有关重定向到模型函数或remoteMethod. 这里有人已经这样做了吗?请在下面找到我的代码。
Form.catch = function (id, data, cb) {
Form.findById(id, function (err, form) {
if (form) {
form.formentries.create({"input": data},
function(err, result) {
/*
Below i want the callback to redirect to a url
*/
cb(null, "http://google.be");
});
} else {
/*
console.log(err);
*/
let error = new Error();
error.message = 'Form not found';
error.statusCode = 404;
cb(error);
}
});
};
Form.remoteMethod('catch', {
http: {path: '/catch/:id', verb: 'post'},
description: "Public endpoint to create form entries",
accepts: …Run Code Online (Sandbox Code Playgroud) 在IE11及以下版本上查看我的应用时出现此错误.该应用程序还返回一个白页.
更糟糕的是,控制台没有显示行号来指示问题的位置/位置.这使得很难弄清楚出了什么问题.我正在使用Webpack 4和babel-polyfill.
谁能指出我正确的方向?
const HtmlWebPackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require('webpack');
const extractSass = new ExtractTextPlugin({
filename: "[name].[hash].css",
disable: process.env.NODE_ENV === "development"
});
module.exports = {
// mode: 'production',
entry: [
'babel-polyfill',
'./src/index.js'
],
output: {
publicPath: '/',
filename: '[name].[hash].js',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [{
loader: "file-loader",
options: {
// …Run Code Online (Sandbox Code Playgroud) javascript ×2
express ×1
loopback ×1
loopbackjs ×1
nestjs ×1
reactjs ×1
typeorm ×1
typescript ×1
webpack ×1