Javascript node js中的异步编程通过mongoose模型传递常量/预定义的强制值
我有多个问题,请检查我的代码 1) 如何通过模型传递常量/预定义的强制值 例如,我有一些字段,用户必须在kafkaSchema.config[]和livySchema.args[]中传递值和一些常量。我要传递的代码在同一问题线程的第二个问题中Javascript node js中的异步编程通过mongoose模型传递常量/预定义的强制值,javascript,node.js,express,asynchronous,mongoose,Javascript,Node.js,Express,Asynchronous,Mongoose,我有多个问题,请检查我的代码 1) 如何通过模型传递常量/预定义的强制值 例如,我有一些字段,用户必须在kafkaSchema.config[]和livySchema.args[]中传递值和一些常量。我要传递的代码在同一问题线程的第二个问题中 const mongoose = require('mongoose'); const livy_schema = mongoose.Schema({ file: { type: String, required: true }, n
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) 如何使这段代码异步运行,目前它是同步运行的。例如,它可以在数据库的事件集合中保存eventdata,然后更新项目集合,然后调用axios.post方法依次调用my livy server和kafka server。我要做的是将eventdata保存在数据库中的EventCollection中,然后(同步)更新项目集合,同时(异步)调用我的livy和kafka服务器
这个问题可能看起来有点冗长,但这样问是有道理的。请引导我到正确的方向。请尝试以下方法:
1) 用于保存用户输入的配置,并具有默认常量。您可以使用mongoosepre-save
hook
2) 对于第二个问题,请尝试以下内容:
axios.all([
axios.post("http://52.221.178.199:8998/batches", result.livy),
axios.post("http://52.221.178.199:8083/connectors", result.kafka)
]);
(一)
(二)
这是第二个问题。工作正常,你能回答问题1吗?@darshanan我已经更新了我的答案,请看看这是否能解决你的问题。请将问题分成两个独立的问题,范围更为有限。你的问题和现在写的一样。@zero298好了,伙计。。。而且还要投票表决这个问题,因为。。。我正处于被禁止进入SOI的边缘我没想到@vkarpov15会回答我的问题。。。实际上,第一个问题是关于每次发布数据时是否可以传递一些硬编码的值。我想你已经在代码中看到了我的注释并回答了它,这也是我所必需的。无论如何,我将编辑该问题。此线程中的第一个问题已重新发布。第一个答案不起作用,livy:{type:[livy_schema],required:true}。请提供另一种解决方案。
livy_schema.pre('save', function(next) {
this.args = { ...this.args, ...CONSTANTS }; //I'm use es6's spread operator
next();
});
kafka_schema.pre('save', function(next) {
this.config = { ...this.config, ...CONSTANTS }; //I'm use es6's spread operator
next();
});
axios.all([
axios.post("http://52.221.178.199:8998/batches", result.livy),
axios.post("http://52.221.178.199:8083/connectors", result.kafka)
]);
const enrichedEventSchema = mongoose.Schema({
// ...
livy: { type: [livy_schema], required: true }
});
return enrichedEvent.save().
then(result => {
// ...
return Project.findOneAndUpdate(/*...*/);
}).
then(() => {
// ...
return Promise.all([axios.post(/*...*/), axios.post(/*...*/]);
});