Node.js 为什么我使用nodejs child_进程fork函数时,mongoose save函数不起作用
首先,我有一个没有子进程的nodejs项目和mongodb。它工作正常,我得到数据并插入mongodb 我的模式Node.js 为什么我使用nodejs child_进程fork函数时,mongoose save函数不起作用,node.js,mongodb,mongoose,child-process,Node.js,Mongodb,Mongoose,Child Process,首先,我有一个没有子进程的nodejs项目和mongodb。它工作正常,我得到数据并插入mongodb 我的模式 const mongoose = require('mongoose'); var Schema = mongoose.Schema; var TestSchema = new Schema({ ID:String, username:String, full_name:String }); module.exports = mongoose.model('Test'
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TestSchema = new Schema({
ID:String,
username:String,
full_name:String
});
module.exports = mongoose.model('Test',TestSchema);
插入数据
let testInsert = {
ID:1,
username:'test_user',
full_name:'test_full'
};
let test = new Test(testInsert);
test.save(err => {
if(err){
console.log(err);
} else{
console.log('insert')
}
});
这是正确的,但当我使用child\u process test.save时,它不起作用,甚至不进入。在没有看到任何其他代码的情况下,我只能假设问题是进程在MongoDB之前完成。尝试更改代码以等待
保存
完成,例如
(async () => {
try {
const test = new Test({
ID:1,
username:'test_user',
full_name:'test_full'
});
await test.save();
} catch (e) {
console.error(e);
}
})()
我不明白为什么不进入test.save(),会调用@VahagnAleksanyan
save
,但应用程序可能退出得太快,以至于没有时间到达或完成MongoDB调用,正如我所说,没有其他代码可以查看这是我的倾向。你试过我的代码吗?是的,我试过了,但不起作用。当我来到child_过程时,它是真的。