Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么我使用nodejs child_进程fork函数时,mongoose save函数不起作用_Node.js_Mongodb_Mongoose_Child Process - Fatal编程技术网

Node.js 为什么我使用nodejs child_进程fork函数时,mongoose save函数不起作用

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'

首先,我有一个没有子进程的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',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_过程时,它是真的。