Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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文档插入的多个数组_Node.js_Mongodb_Promise_Foreign Keys - Fatal编程技术网

Node.js 如何保证所有mongoose文档插入的多个数组

Node.js 如何保证所有mongoose文档插入的多个数组,node.js,mongodb,promise,foreign-keys,Node.js,Mongodb,Promise,Foreign Keys,我试图使用Mongoose模型将文档插入MongoDB,我希望使用Promissions。由于此数据来自MSSQL数据库,因此每个集合中的文档与其他集合中的文档具有外键关系 下面是示例代码: const Promise = require('bluebird'); const mongoose = require('mongoose'); const Make = Promise.promisifyAll(require('../models/make-model')); const Namep

我试图使用Mongoose模型将文档插入MongoDB,我希望使用Promissions。由于此数据来自MSSQL数据库,因此每个集合中的文档与其他集合中的文档具有外键关系

下面是示例代码:

const Promise = require('bluebird');
const mongoose = require('mongoose');
const Make = Promise.promisifyAll(require('../models/make-model'));
const Nameplate = Promise.promisifyAll(require('../models/nameplate-model'));
const Vehicle = Promise.promisifyAll(require('../models/vehicle-model'));

const { makes, nameplates, vehicles } = require('./sqlExport.json');

const logErr = (err) => console.error(err);

Promise.resolve(
  mongoose.connection.db.dropDatabase()
)
.then(() => (
  Promise.all(
    makes.map(make => (
      Make.create(make).then(makeRes => console.log(`Created make ${makeRes}`))
    ))
  )),
  logErr
)
.then(() => (
  Promise.all(
    nameplates.map(nameplate => {
      const { sqlMakeId, sqlModelId, sqlSubModelId } = nameplate;
      return Make.find({ sqlMakeId })
        .then(makeRes => {
          if (makeRes.length !== 1) throw new Error(`duplicate or non-existent key for make.sqlMakeId: ${sqlMakeId}`);
          const finalNameplate = Object.assign({}, nameplate, { mongoMakeId: makeRes[0]._id });
          return Nameplate.create(finalNameplate);
        })
        .then(nameplateRes => {
          console.log(`Created nameplate ${nameplateRes}`);
        });
    })
  )),
  logErr
)
.then(() => (
  Promise.all(
    vehicles.map(vehicle => {
      const { sqlVehicleId, sqlMakeId, sqlSubModelId } = vehicle;
      const extraParams = {};
      return Make.find({ sqlMakeId })
        .then(makeRes => {
          if (makeRes.length !== 1) throw new Error(`duplicate or non-existent key for make.sqlMakeId: ${sqlMakeId}`);
          extraParams.mongoMakeId = makeRes[0]._id;
          return Nameplate.find({ sqlSubModelId });
        })
        .then(nameplateRes => {
          if (makeRes.length !== 1) throw new Error(`duplicate or non-existent key for nameplate.sqlSubModelId: ${sqlSubModelId}`);
          extraParams.mongoNameplateId = nameplateRes[0]._id;
          const finalVehicle = Object.assign({}, vehicle, extraParams);
          return Vehicle.create(finalVehicle);
        })
        .then(vehicleRes => {
          console.log(`Created vehicle ${vehicleRes}`);
        });
    })
  )),
  logErr
)
.catch(logErr);
问题是,有时车辆插页都在任何铭牌插页之前运行,因此无法找到mongoNameplateId的正确文档id


完整的代码如下:

我在mongoose和bluebird require语句下面添加了这一行,使其正常工作:

mongoose.Promise=Promise

此处的文档:


在这种情况下,不需要蓝鸟的
promisifyAll
模式。

看起来应该对我有用。如果你能把代码精简到最简单的部分,使调试变得更容易,这会有所帮助。我喜欢做的一件事是用setTimeout调用替换所有异步调用,这些调用在大约一秒钟后解析承诺,并打印一条消息,说明它们要替换的内容,即
console.log(“Make.find('blah'))
铭牌
车辆
API已发布,但没有证据表明已调用发布的方法。是否应
铭牌.create()
铭牌.createAsync()
,以及
车辆.create()
车辆.createAsync()
?@Roamer-1888呼叫正确。我从未使用过bluebird,但查看它声明的文档
注意,对象上的原始方法没有被覆盖,而是使用异步后缀创建了新方法。例如,如果promisifyAll node.js fs对象使用fs.statAsync调用promisified stat方法。
非常简单,但是如果这样做有效,那么默认的
mpromise
似乎也会起作用。你有没有试过使用Bluebird和mongoose.Promise=Promise
行?@Roamer-1888确实可以使用mpromise,但是Bluebird比mpromise性能更好。我不得不用typescript这样说:
(mongoose)。Promise=Promise