我需要从这个类访问上下文,以便我可以从数据库中检查一些数据,但我不知道如何将它传输到下面的服务:
internal class TimedHostedService : IHostedService, IDisposable
{
private readonly ILogger _logger;
private Timer _timer;
public TimedHostedService(ILogger<TimedHostedService> logger) //context ?
{
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is starting.");
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(60));
return Task.CompletedTask;
}
private void DoWork(object state)
{
_logger.LogInformation("Atualização automática");
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is stopping.");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
启动文件:
namespace Products
{
public …Run Code Online (Sandbox Code Playgroud) 我有 Encomenda,它有一个 Itens 数组。Itens 可以有一个 itens 数组。我有以下代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var idvalidator = require('mongoose-id-validator');
let ItemSchema = new Schema({
produtoId:Number,
itens:[{type:Schema.Types.ObjectId, ref: 'Item'}]
});
function autoPopulateItens(next){
this.populate('itens');
next();
}
ItemSchema
.pre('findOne',autoPopulateItens)
.pre('find',autoPopulateItens);
module.exports=mongoose.model('Item',ItemSchema);
let EncomendaSchema= new Schema({
itens:[ItemSchema]
});
module.exports=mongoose.model('Encomenda',EncomendaSchema);
function log(data){
console.log(JSON.stringify(data,undefined,2))
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试向邮递员发出帖子请求,以使用以下代码创建一个 Encomenda:
{
"itens":[{
"produtoId":5,
"itens":[{
"produtoId":6,
"itens":[]
},{
"produtoId":7,
"itens":[]
},{
"produtoId":8,
"itens":[]
}]
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试创建一个 Encomenda 并将其保存到 mongodb 时,它给了我一个错误:
CoreMongooseArray [ { itens: [], _id: 5bd9fde20c29e35f2c0ca74a, produtoId: …Run Code Online (Sandbox Code Playgroud) asp.net-core ×1
c# ×1
dbcontext ×1
javascript ×1
mongodb ×1
mongoose ×1
node.js ×1
scope ×1
service ×1