gpt4 book ai didi

javascript - Node js中的异步编程通过 Mongoose 模型传递常量/预定义的强制值

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:00 25 4
gpt4 key购买 nike

我有多个问题,请查看我的代码。

1) 如何通过模型传递常量/预定义强制值?

例如。我有一些用户必须传递值的字段和一些要在 kafkaSchema.config[ ] 和 livySchema.args[ ] 中传递的常量。我要传递的代码在同一个问题线程的第二个问题中。

 const mongoose = require('mongoose');


const livy_schema = mongoose.Schema({
file: { type: String, required: true },
name: { type: String, required: true },
className: { type: String, required: true },
args: [{ type: mongoose.Schema.Types.Mixed, required: true }] //here i have constants to pass on to
});

const kafka_schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true, unique: false },
config: { type: mongoose.Schema.Types.Mixed, required: true } //here i have constants to pass on to
});


const enrichedEventSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
name: { type: String, required: true, unique: true },
description: { type: String, required: false },
type: { type: String, enum: ["Enriched"], required: true },
format: { type: String, enum: ["JSON", "DELIMITED", "FixedWidth", "LOG"], required: true },
kafka: [kafka_schema],
livy: [livy_schema] // how to make this schema required:true?
});

module.exports = mongoose.model('EnrichedEvent', enrichedEventSchema);

2) 如何让这段代码异步运行,现在它是同步运行的。例如,它能够将事件数据保存在数据库中的事件集合中,然后更新项目集合,然后调用 axios.post 方法依次调用我的 livy 服务器和 kafka 服务器。我想要做的是将事件数据保存在数据库中的事件集合中,然后更新项目集合(同步),同时我想同时(异步)调用我的 livy 和 kafka 服务器。

router.post("/:projectId/events/enriched", (req, res, next) => {
const enrichedEvent = new EnrichedEvent({
_id: mongoose.Types.ObjectId(),
name: req.body.name,
projectId: req.params.projectId, //taking from url
description: req.body.description,
type: req.body.type,
format: req.body.format,
kafka: req.body.kafka,
livy: req.body.livy
});
enrichedEvent.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Event stored",
createdEvent: {
_id: result._id,
projectId: result.projectId,
name: result.name,
description: result.description,
type: result.type,
kafka: result.kafka,
livy: result.livy
}
});

Project.findOneAndUpdate({ _id: result.projectId },
{ $push: { enrichedEvents: result._id } })
axios.post("http://52.xxx.xxx.199:8998/batches", result.livy)
.then(function (response) {
console.log(response);
})
.then(axios.get("http://52.xxx.xxx.199:8998/batches/"), function (res) {
console.log(res);
})
axios.post("http://52.xxx.xxx.199:8083/connectors", result.kafka)
.then(function (response) {
console.log(response);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
});

问题可能看起来有点冗长,但在 SO 上提出的问题是有效的。请指引我正确的方向。

最佳答案

1)

const enrichedEventSchema = mongoose.Schema({
// ...
livy: { type: [livy_schema], required: true }
});

2)

return enrichedEvent.save().
then(result => {
// ...
return Project.findOneAndUpdate(/*...*/);
}).
then(() => {
// ...
return Promise.all([axios.post(/*...*/), axios.post(/*...*/]);
});

关于javascript - Node js中的异步编程通过 Mongoose 模型传递常量/预定义的强制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51836461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com