这是我的MongoDB模式:
var partnerSchema = new mongoose.Schema({
name: String,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}]
});
var productSchema = new mongoose.Schema({
name: String,
campaign: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Campaign'
}
]
});
var campaignSchema = new mongoose.Schema({
name: String,
});
module.exports = {
Partner: mongoose.model('Partner', partnerSchema),
Product: mongoose.model('Product', productSchema),
Campaign: mongoose.model('Campaign', campaignSchema)
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何在考虑引用的情况下从任何文档中删除对象(也许我应该以某种方式使用猫鼬填充)?例如,如果我要删除,Product
那么我假设我还将也删除ref ID Partner
和所有Campaigns
属于this的引用ID Product
。
目前,我以这种方式删除:
var campSchema = require('../model/camp-schema');
router.post('/removeProduct', function (req, res) {
campSchema.Product.findOneAndRemove({ _id: req.body.productId }, function …
Run Code Online (Sandbox Code Playgroud) 这是我的对象:
var obj = {
"flag": false,
"text": "good text",
"someArray": [
{
"questionId": 11,
"text": "qwe",
"TypeId": "B",
"source": "oooooo"
},
{
"questionId": 12,
"text": "zxc",
"TypeId": "A",
"source": "pppppp"
},
{
"questionId": 13,
"text": "asd",
"TypeId": "D",
"source": "cccccc"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是从someArray
属性TypeId
和source
. 我怎样才能用 lodash 做到这一点?我正在尝试使用 _.pick,但我不知道如何在里面使用它someArray
。
是否可以在Class上添加覆盖所有空字符串属性的自定义属性?像这样:
[DefaultValueForEmptyString(Text="N/A")]
public class PersonsDTO
{
public string Name { get; set; }
public string Lastname { get; set; }
public string Address { get; set; }
}
public class DefaultValueForEmptyString
{
public static void MapProperties(object Properties, string text)
{
foreach (var property in Properties)
{
if(string.IsNullOrEmpty(property))
{
property = text // "N/A in this case
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何异步实现Dapper。我发现了这个
看起来很酷,但我注意到该调用api链中的所有方法都使用了async / await:
控制器:
[HttpGet]
[Route("dob/{dateOfBirth}")]
public async Task<ActionResult<List<Employee>>> GetByID(DateTime dateOfBirth)
{
return await _employeeRepo.GetByDateOfBirth(dateOfBirth);
}
Run Code Online (Sandbox Code Playgroud)
仓库:
public async Task<List<Employee>> GetByDateOfBirth(DateTime dateOfBirth)
{
using (IDbConnection conn = Connection)
{
string sQuery = "SELECT ID, FirstName, LastName, DateOfBirth FROM Employee WHERE DateOfBirth = @DateOfBirth";
conn.Open();
var result = await conn.QueryAsync<Employee>(sQuery, new { DateOfBirth = dateOfBirth });
return result.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我们之间要有更多的层,例如服务等,那么这些层也应该具有异步/等待功能?我认为这是多余的,完全可以在需要真正返回异步结果的方法中使用异步/等待。在其他地方,例如Controller,Services-仅Task是必需的。
可以在我们使用处理整个上下文的块的地方放置异常。因此,我们还必须具有异步/等待功能。
我是对的,还是我想念什么?
@编辑
因此控制器可以另存为:
[HttpGet]
[Route("dob/{dateOfBirth}")]
public Task<ActionResult<List<Employee>>> GetByID(DateTime dateOfBirth)
{
return _employeeRepo.GetByDateOfBirth(dateOfBirth);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我删除了async / await-现在只有存储库具有async / …
c# ×2
.net ×1
asp.net ×1
async-await ×1
asynchronous ×1
javascript ×1
lodash ×1
mongodb ×1
mongoose ×1
node.js ×1