Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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 为什么不使用mongoose将完整的JSON发送到mongodb?_Node.js_Json_Angular_Mongodb_Mongoose - Fatal编程技术网

Node.js 为什么不使用mongoose将完整的JSON发送到mongodb?

Node.js 为什么不使用mongoose将完整的JSON发送到mongodb?,node.js,json,angular,mongodb,mongoose,Node.js,Json,Angular,Mongodb,Mongoose,我在网页上有一个功能,可以将excel文件上传到Node并将其解析为JSON,然后将数据传递给mongodb。它只向mongodb发送一个文档,每个文档都组织在一个数组中,并使用for循环遍历每个文档,因此唯一发送的是第一个文档。我还尝试使用model.create(docs)函数,该函数将每个文档发送到数据库,但问题是相同的。这是代码(model.create(docs)位于////)中: app.post('/upload',函数(req,res){ var-exceltoson; 上传(请

我在网页上有一个功能,可以将excel文件上传到Node并将其解析为JSON,然后将数据传递给mongodb。它只向mongodb发送一个文档,每个文档都组织在一个数组中,并使用for循环遍历每个文档,因此唯一发送的是第一个文档。我还尝试使用model.create(docs)函数,该函数将每个文档发送到数据库,但问题是相同的。这是代码(model.create(docs)位于////)中:

app.post('/upload',函数(req,res){
var-exceltoson;
上传(请求、恢复、功能(错误){
如果(错误){
res.json({error\u code:1,err\u desc:err})
返回;
}
如果(!req.file){
res.json({error_code:1,err_desc:“未传递文件”});
返回;
}
if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1]==='xlsx'){
exceltojson=xlsxtojson;
}否则{
exceltojson=xlstojson;
}
试一试{
exceltojson({
输入:req.file.path,
输出:“./outPutJSON/output.json”,
小写标题:true
},函数(错误,结果){
如果(错误){
返回res.json({error\u code:1,err\u desc:err,data:null});
}
res.json({datos:{Los datos fueron agregados exitosamente});
//res.json({error\u code:0,err\u desc:null,data:result});
让resultDos=fs.readFile(“./outPutJSON/output.json“,'utf8',(err,fileContents)=>{
如果(错误){
控制台错误(err)
返回;
}
试一试{
const data=JSON.parse(fileContents)
console.log(data.length);
//////////////////////////////////////////////////
模型.创建(数据,函数(err){
如果(错误){
控制台日志(err);
}
});
///////////////////////////////////////////////////
//对于(var cantidad=0;cantidad
这是JSON文件:

只有这一个被发送-->{“nombre”:“Wilson Junior Toribio”,“cedula”:“4022589632”,“direccion”:“Calle 7#33布宜诺斯艾利斯”}

{“nombre”:“Jose Luis Toribio”,“cedula”:“4023495023”,“direccion”:“Calle 11#69布宜诺斯艾利斯”}

{“nombre”:“Joel de Jesus Toribio”,“cedula”:“4023548902”,“direccion”:“Calle 1#3 Buenos Aires”}

{“nombre”:“Corazon Roa”,“cedula”:“4026984452”,“direccion”:“Calle 3#19布宜诺斯艾利斯”}

我甚至输出每个文档以验证文档是否存储在变量中,这是输出:


问题已经解决,我必须使用async和Wait将回调编辑为同步,并使用let在for中声明变量cantidad:

let resultDos = fs.readFile("./outPutJSON/output.json", 'utf8', -> async (err, fileContents) => {
                if (err) {
                  console.error(err)
                  return;
                }
                try {
                  let data = JSON.parse(fileContents)
                  console.log(data.length);

                  console.log(data);
                //   model.create(data, function (err) {
                //       if(err){
                //           console.log(err);
                //       }
                //   });

                  for(let cantidad = 0; cantidad < data.length; cantidad++){
                    var documento = data[cantidad];
                    var mod = new model(documento);
                    console.log(documento);
                 -> await mod.save(documento);
                    // model.create(documento).save();
                  }
let resultDos=fs.readFile(“./outPutJSON/output.json“,'utf8',->async(err,fileContents)=>{
如果(错误){
控制台错误(err)
返回;
}
试一试{
让data=JSON.parse(fileContents)
console.log(data.length);
控制台日志(数据);
//模型.创建(数据,函数(err){
//如果(错误){
//控制台日志(err);
//       }
//   });
for(设cantidad=0;cantidad等待修改保存(documento);
//model.create(documento.save();
}

您在控制台中有任何错误吗?没有,它没有显示任何错误。您可以对其进行字符串化并打印吗?我对documento变量进行了字符串化,我得到了这个错误:Document()的参数“obj”必须是一个对象,得到了{“nombre”:“Wilson Junior Toribio”,“cedula”:“4022589632”,“direcion”:“Calle 7#33 Buenos Aires”}
let resultDos = fs.readFile("./outPutJSON/output.json", 'utf8', -> async (err, fileContents) => {
                if (err) {
                  console.error(err)
                  return;
                }
                try {
                  let data = JSON.parse(fileContents)
                  console.log(data.length);

                  console.log(data);
                //   model.create(data, function (err) {
                //       if(err){
                //           console.log(err);
                //       }
                //   });

                  for(let cantidad = 0; cantidad < data.length; cantidad++){
                    var documento = data[cantidad];
                    var mod = new model(documento);
                    console.log(documento);
                 -> await mod.save(documento);
                    // model.create(documento).save();
                  }