I have several models:
我有几个型号:
- CountryModel - no references,
- PersonModel - has a phone_id reference,
- PhoneModel - has a country_code reference
CountryModel - 没有引用,
PersonModel - 有一个phone_id参考,
PhoneModel - 有country_code参考
With the code below, I have been able to seed PhoneModel with a country_code reference, but when I attempt to push the phone_ids, afterwards, into the PersonModel, the array is always empty.
使用下面的代码,我已经能够使用country_code引用为PhoneModel播种,但是当我尝试将phone_ids推送到PersonModel之后,该数组始终为空。
What do I need to change in code below to retain the phone_ids in the person document?
我需要在下面的代码中更改以保留person文档中的phone_ids?
import * as commonPhone from '../../_common/communication/phone.common.actions.api';
function seedDatabase() {
return new Promise((resolve, reject) => {
PersonModel.find({}, (errFind, collection) => {
if (errFind) {
console.log(`${PersonModel.modelName} Error: ${errFind.message}`);
reject(errFind);
}
if (collection.length === 0) {
data.map(person => {
const newPerson = Object.assign({phone_ids: []}, person);
person.phone_numbers.map(phone => {
// successfully seeded commonPhone collection
commonPhone.addNew(phone, (errPhone, newPhone) => {
if (errPhone) {
console.log(`${commonPhone.modelName} Error: ${errPhone.message}`);
reject(errPhone);
}
resolve(newPhone);
// newPerson does show newPhone.id added to array
newPerson.phone_ids.push(newPhone.id);
});
})
//////////////// PROBLEM IS HERE ////////////////
// the array for person's phone_ids is empty?!?!?!
console.log('PhoneIds: ' + JSON.stringify(newPerson.phone_ids));
});
}
});
});
}
1 个解决方案
#1
0
Figured this one out after two days. The solution may be unique but I think it will be helpful for anyone involved with mapping ES6 promises and dealing with mongoose models that have references derived from these ES6 promises, so I have been quite detailed about a solution.
两天后想出这个。解决方案可能是独一无二的,但我认为这对参与映射ES6承诺和处理从这些ES6承诺中获得参考的猫鼬模型的任何人都有帮助,因此我对解决方案非常详细。
I. Scenario:
1.The PersonModel has a collection phone_ids from the PhoneModel.
1. PersonModel有一个来自PhoneModel的集合phone_ids。
2.The PhoneModel requires a country_id and has a country_code.
2. PhoneModel需要country_id并且有country_code。
3.The CountryModel has a country._id property that the PhoneModel needs and a country_code.
3. CountryModel具有PhoneModel需要的country._id属性和country_code。
II. The Easy Part:
II。简单部分:
In my redux actions that I have as api calls, here are the Country get method and Phone addNew method.
在我作为api调用的redux操作中,这里是Country get方法和Phone addNew方法。
a. country actions ...
一个。国家行动......
import {CountryRefModel} from '../../../models/_repo.models.api';
module.exports {
.....
// need country name too, Canada/US/Carribean Is. have same country_code
getIdByCountryCode: (countryName, countryCode) => {
return new Promise((resolve, reject) => {
CountryRefModel.findOne({name: countryName, country_code: countryCode },
{_id: 1}, (err, result) => {
if (err) {
console.log(`${CountryModel.modelName} getIdByCountryCode Error: ${err.message}`);
reject(err);
}
resolve(result);
});
});
},
.....
}
a. phone actions ...
一个。电话行动......
import {PhoneModel} from '../../../models/_repo.models.api';
import * as refCountry from '../../_reference/country/country.reference.actions.api';
module.exports {
....
addNew: (phone) => {
return new Promise((resolve, reject) => {
refCountry.getIdByCountryCode(phone.country.name, phone.country.country_code)
// using then method as country action get method
// is already a promise
.then(returnValue => {
PhoneModel.create({
_id: phone.id,
country_id: returnValue,
type: phone.type,
isMain: phone.isMain ? phone.isMain : false,
area_code: phone.area_code,
prefix: phone.prefix,
suffix: phone.suffix,
vanity_number: phone.vanity_number ? phone.vanity_number : null
}, (err, result) => {
if (err) {
console.log(`${PersonModel.modelName} addNew Error: ${err.message}`);
reject(err);
}
resolve(result);
});
});
});
},
....
}
II. Dicey Part:
II。冒险部分:
The person actions has seed and addNew methods for what I need to do.
人员行为有种子,并为我需要做的添加新方法。
I need to map thru the people,
我需要通过人民来映射,
Map thru their phones,
通过手机映射,
a. get the country_id for each phone
一个。获取每部手机的country_id
b. add each phone to the phone collection
湾将每部手机添加到手机集中
c. pass the respective phone._id's to the person
C。将相应的phone._id传递给该人
Add a new person to the collection.
将新人添加到集合中。
a. person actions ....
一个。人的行为....
The difficult part that took me a while to figure out was the seed method.
花了一段时间才弄明白的困难部分是种子方法。
Points to consider:
需要考虑的要点:
Assigning your mappings to a variable.
将映射分配给变量。
Since the phone action's addNew does the CountryModel search in addition to adding a new phone, I only need to call it's addNew method in the mapping.
由于手机动作的addNew除了添加新手机外还进行了CountryModel搜索,我只需要在映射中调用它的addNew方法。
I then call the addNew method with the same module, via module.exports.addNew'
然后我通过module.exports.addNew调用addNew方法和相同的模块
import mongoose from 'mongoose';
import {PersonModel} from '../../../models/_repo.models.api';
import {addNew} from '../../_common/communication/phone.common.actions.api';
const data = require('../../../seed/_common/people/person.seed.api.js').getData();
function seedDatabase() {
PersonModel.find({}, (errFind, collection) => {
if (errFind) {
console.log(`${PersonModel.modelName} Seed Error: ${errFind.message}`);
}
if (collection.length === 0) {
const updatedPerson = data.map(person => {
person.id = mongoose.Types.ObjectId();
const phoneIds = [];
const updatePhones = person.phone_numbers.map(phone => {
phone.id = mongoose.Types.ObjectId();
phoneIds.push(phone.id);
return addNew(phone);
});
return module.exports.addNew(person, phoneIds);
});
});
// Just an example of taking the promises further
// return LocationModel.addNew(person.id);
}
});
}
module.exports = {
seed: () => seedDatabase(),
....
addNew: (person, phoneIds) => {
return new Promise((resolve, reject) => {
person.phone_ids = (phoneIds);
PersonModel.create({
prefix: person.prefix,
fName: person.fName,
mName: person.mName,
lName: person.lName,
suffix: person.suffix,
profDes: person.profDes,
gender: person.gender,
email: person.email,
phone_ids: person.phone_ids,
lang_key: person.lang_key
}, (err, result) => {
if (err) {
console.log(`${PersonModel.modelName} addNew Error: ${err.message}`)
reject(err);
} else {
resolve(result);
}
})
})
},
....
}
As usual, everything in documentation is overly simplified and not abundantly clear on real world scenarios. Hope this helps people!
像往常一样,文档中的所有内容都过于简化,并且在实际场景中并不十分清晰。希望这有助于人们!
#1
0
Figured this one out after two days. The solution may be unique but I think it will be helpful for anyone involved with mapping ES6 promises and dealing with mongoose models that have references derived from these ES6 promises, so I have been quite detailed about a solution.
两天后想出这个。解决方案可能是独一无二的,但我认为这对参与映射ES6承诺和处理从这些ES6承诺中获得参考的猫鼬模型的任何人都有帮助,因此我对解决方案非常详细。
I. Scenario:
1.The PersonModel has a collection phone_ids from the PhoneModel.
1. PersonModel有一个来自PhoneModel的集合phone_ids。
2.The PhoneModel requires a country_id and has a country_code.
2. PhoneModel需要country_id并且有country_code。
3.The CountryModel has a country._id property that the PhoneModel needs and a country_code.
3. CountryModel具有PhoneModel需要的country._id属性和country_code。
II. The Easy Part:
II。简单部分:
In my redux actions that I have as api calls, here are the Country get method and Phone addNew method.
在我作为api调用的redux操作中,这里是Country get方法和Phone addNew方法。
a. country actions ...
一个。国家行动......
import {CountryRefModel} from '../../../models/_repo.models.api';
module.exports {
.....
// need country name too, Canada/US/Carribean Is. have same country_code
getIdByCountryCode: (countryName, countryCode) => {
return new Promise((resolve, reject) => {
CountryRefModel.findOne({name: countryName, country_code: countryCode },
{_id: 1}, (err, result) => {
if (err) {
console.log(`${CountryModel.modelName} getIdByCountryCode Error: ${err.message}`);
reject(err);
}
resolve(result);
});
});
},
.....
}
a. phone actions ...
一个。电话行动......
import {PhoneModel} from '../../../models/_repo.models.api';
import * as refCountry from '../../_reference/country/country.reference.actions.api';
module.exports {
....
addNew: (phone) => {
return new Promise((resolve, reject) => {
refCountry.getIdByCountryCode(phone.country.name, phone.country.country_code)
// using then method as country action get method
// is already a promise
.then(returnValue => {
PhoneModel.create({
_id: phone.id,
country_id: returnValue,
type: phone.type,
isMain: phone.isMain ? phone.isMain : false,
area_code: phone.area_code,
prefix: phone.prefix,
suffix: phone.suffix,
vanity_number: phone.vanity_number ? phone.vanity_number : null
}, (err, result) => {
if (err) {
console.log(`${PersonModel.modelName} addNew Error: ${err.message}`);
reject(err);
}
resolve(result);
});
});
});
},
....
}
II. Dicey Part:
II。冒险部分:
The person actions has seed and addNew methods for what I need to do.
人员行为有种子,并为我需要做的添加新方法。
I need to map thru the people,
我需要通过人民来映射,
Map thru their phones,
通过手机映射,
a. get the country_id for each phone
一个。获取每部手机的country_id
b. add each phone to the phone collection
湾将每部手机添加到手机集中
c. pass the respective phone._id's to the person
C。将相应的phone._id传递给该人
Add a new person to the collection.
将新人添加到集合中。
a. person actions ....
一个。人的行为....
The difficult part that took me a while to figure out was the seed method.
花了一段时间才弄明白的困难部分是种子方法。
Points to consider:
需要考虑的要点:
Assigning your mappings to a variable.
将映射分配给变量。
Since the phone action's addNew does the CountryModel search in addition to adding a new phone, I only need to call it's addNew method in the mapping.
由于手机动作的addNew除了添加新手机外还进行了CountryModel搜索,我只需要在映射中调用它的addNew方法。
I then call the addNew method with the same module, via module.exports.addNew'
然后我通过module.exports.addNew调用addNew方法和相同的模块
import mongoose from 'mongoose';
import {PersonModel} from '../../../models/_repo.models.api';
import {addNew} from '../../_common/communication/phone.common.actions.api';
const data = require('../../../seed/_common/people/person.seed.api.js').getData();
function seedDatabase() {
PersonModel.find({}, (errFind, collection) => {
if (errFind) {
console.log(`${PersonModel.modelName} Seed Error: ${errFind.message}`);
}
if (collection.length === 0) {
const updatedPerson = data.map(person => {
person.id = mongoose.Types.ObjectId();
const phoneIds = [];
const updatePhones = person.phone_numbers.map(phone => {
phone.id = mongoose.Types.ObjectId();
phoneIds.push(phone.id);
return addNew(phone);
});
return module.exports.addNew(person, phoneIds);
});
});
// Just an example of taking the promises further
// return LocationModel.addNew(person.id);
}
});
}
module.exports = {
seed: () => seedDatabase(),
....
addNew: (person, phoneIds) => {
return new Promise((resolve, reject) => {
person.phone_ids = (phoneIds);
PersonModel.create({
prefix: person.prefix,
fName: person.fName,
mName: person.mName,
lName: person.lName,
suffix: person.suffix,
profDes: person.profDes,
gender: person.gender,
email: person.email,
phone_ids: person.phone_ids,
lang_key: person.lang_key
}, (err, result) => {
if (err) {
console.log(`${PersonModel.modelName} addNew Error: ${err.message}`)
reject(err);
} else {
resolve(result);
}
})
})
},
....
}
As usual, everything in documentation is overly simplified and not abundantly clear on real world scenarios. Hope this helps people!
像往常一样,文档中的所有内容都过于简化,并且在实际场景中并不十分清晰。希望这有助于人们!