gpt4 book ai didi

javascript - 我正在尝试将数组映射到另一个映射函数中我的代码未同步运行

转载 作者:行者123 更新时间:2023-11-30 19:39:36 27 4
gpt4 key购买 nike

我有一组潜在客户详细信息(我需要将其插入数据库)并且每个潜在客户都有一个名为 interestArea 的字段,它是一个字符串数组,我必须找到每个 interestArea,如果从数据库中找到了 interestArea,我将不得不存储 interestArea 的 ObjectId,否则我将不得不在 db 中创建新的 interestArea 并将其 objectId 作为潜在客户的字段存储在 interestArea 中

我的意见

let jsonArray = [ { firstName: 'newLead1',
lastName: 'newLead1',
company: 'hub',
companySize: '22',
designation: 'software Enginner',
phoneNumber: '1234567890',
jobRole: 'engineer',
email: 'newLead1@hub.com',
leadSource: 'online',
industry: 'it',
location: 'ernakulam',
annualTurnOver: '10',
requestedMeetingDate: '2019-03-28T09:13:02.958Z',
interestArea: 'app,newspaper' },

{ firstName: 'newLead2',
lastName: 'Sharma',
company: 'hub',
companySize: '20',
designation: 'software Enginner',
phoneNumber: '1234567891',
jobRole: 'engineer',
email: 'newLead2@hub.com',
leadSource: 'web',
industry: 'software',
location: 'kacherippady',
annualTurnOver: '15',
requestedMeetingDate: '2019-03-28T09:13:02.958Z',
interestArea: 'website,newspaper' } ]

下面是我试过的功能

function jsonArrayMap(jsonArray){
return new Promise(function(resolve,reject){
let validateInFn = [];
(async ()=>{
validateInFn = await jsonArray.map(lead => {
(async ()=>{
console.log("step: 1");
console.log("interestArea is present",lead.interestArea);
let interestAreaArray = lead.interestArea.split(',');
console.log("interestAreaArray",interestAreaArray);
let interestAreaObjectIdArrayInFn = await interestAreaArray.map(interestArea => {
console.log("step: 2");
console.log("each word interestArea",interestArea)
InterestArea.find({where:{name:interestArea}},(err,interestAreaInDb)=>{
console.log("step: 3");
console.log("search in db got interestAreaInDb",interestAreaInDb);
if(interestAreaInDb.length>0) {
console.log("step: 4");
return interestAreaInDb[0].id
}
else {
console.log("step: 4");
InterestArea.create({name:interestArea},(err,newInterestArea)=>{
return newInterestArea.id
});
} //else
}) //find is there an interestArea with this name
}) //interestAreaArray.map
console.log("interestAreaObjectIdArrayInFn",interestAreaObjectIdArrayInFn)
})();
})
console.log("validateInFn",validateInFn)
})();
}) //promise
} //jsonArrayMap

输出为

step: 1
interestArea is present app,newspaper
interestAreaArray [ 'app', 'newspaper' ]
step: 2
each word interestArea app
step: 2
each word interestArea newspaper
step: 1
interestArea is present website,newspaper
interestAreaArray [ 'website', 'newspaper' ]
step: 2
each word interestArea website
step: 2
each word interestArea newspaper
interestAreaObjectIdArrayInFn [ undefined, undefined ]
interestAreaObjectIdArrayInFn [ undefined, undefined ]
validateInFn [ undefined, undefined ]
step: 3
search in db got interestAreaInDb [ { name: 'app', id: 5ca799d2491c0cd8f415f980 } ]
step: 4
(node:56798) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
step: 3
search in db got interestAreaInDb []
step: 4
step: 3
search in db got interestAreaInDb [ { name: 'website', id: 5ca799d2491c0cd8f415f981 } ]
step: 4
step: 3
search in db got interestAreaInDb []
step: 4

我期待代码在我使用它的地方等待,但它没有像我预期的那样工作

我也尝试过使用 async npm

我使用了他们的 forEachOf 函数,但也没有用

下面是我试过的功能

function jsonArrayMap(jsonArray){
return new Promise(function(resolve,reject){
let validateInFn = [];
async.forEachOf(jsonArray, (lead, key, callbackforEachOfjsonArray) => {
console.log("step: 1");
console.log("interestArea is present",lead.interestArea);
let interestAreaArray = lead.interestArea.split(',');
console.log("interestAreaArray",interestAreaArray);
let interestAreaObjectIdArrayInFn = [];

async.forEachOf(interestAreaArray, (interestArea, key2, callbackforEachOfInterestAreaArray) => {
console.log("step: 2");
console.log("each word interestArea",interestArea)
InterestArea.find({where:{name:interestArea}},(err,interestAreaInDb)=>{
console.log("step: 3");
console.log("search in db got interestAreaInDb",interestAreaInDb);
if(interestAreaInDb.length>0) {
console.log("step: 4");
interestAreaObjectIdArrayInFn.push(interestAreaInDb[0].id)
if(interestAreaArray.length -1 == key2) callbackforEachOfInterestAreaArray()
}
else {
console.log("step: 4");
InterestArea.create({name:interestArea},(err,newInterestArea)=>{
interestAreaObjectIdArrayInFn.push(newInterestArea.id)
if(interestAreaArray.length -1 == key2) callbackforEachOfInterestAreaArray()
});
} //else
}) //find is there an interestArea with this name
}, function (err,result2) {
console.log("result2",result2)
if(jsonArray.length -1 == key) callbackforEachOfjsonArray()
})
},function (err,result) {
console.log("result",result)
})
}) //promise
} //jsonArrayMap

输出为

step: 1
interestArea is present app,newspaper
interestAreaArray [ 'app', 'newspaper' ]
step: 2
each word interestArea app
step: 2
each word interestArea newspaper
step: 1
interestArea is present website,newspaper
interestAreaArray [ 'website', 'newspaper' ]
step: 2
each word interestArea website
step: 2
each word interestArea newspaper
step: 3
search in db got interestAreaInDb [ { name: 'app', id: 5ca799d2491c0cd8f415f980 } ]
step: 4
(node:56939) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
step: 3
search in db got interestAreaInDb [ { name: 'newspaper', id: 5ca894c3aa7a64dddefa2674 } ]
step: 4
step: 3
search in db got interestAreaInDb [ { name: 'website', id: 5ca799d2491c0cd8f415f981 } ]
step: 4
step: 3
search in db got interestAreaInDb [ { name: 'newspaper', id: 5ca894c3aa7a64dddefa2674 } ]
step: 4

最佳答案

您的代码中存在一些错误:

  1. 你构建了一个新的 Promise,尽管你不必这样做;如果您有包装的回调,只需构建一个。

  2. 您确实调用了两个接受回调的函数,但您没有将它们包装在 new Promise 中。

  3. 等待 .map 的结果;这始终是一个空操作,因为 .map 返回一个数组,而 await 仅适用于 Promises。如果你有一个 promise 数组,你可以使用 Promise.all 并将它变成一个解析为数组的 promise;然后你可以await


现在,您的代码无法轻易修复。因此,让我们从头开始。

首先,让我们将回调包装到 promise 中:

const findInterestArea = (name) => 
new Promise((resolve, reject) => InterestArea.find({where: { name }}, (err, result)=> err ? reject(err) : resolve(result));

const createInterestArea = (name) =>
new Promise((resolve, reject) => InterestArea.create({ name }, (err, result) => err ? reject(err) : resolve(result));

现在,下一步的抽象将是一个findOrInsert操作:

async function findOrCreateInterest(name) {
const exists = await findInterestArea(name);
if (exists) return exists;
return await createInterestArea(name);
}

现在您可以轻松地遍历 jsonArray 和每个 interestArea 并将其插入:

async function jsonArrayMap(jsonArray) {
for(const lead of jsonArray) {
const interests = lead.interestArea.split(',');
for (const interest of interests) {
const { id } = await findorCreateInterest(interest);
// do stuff with id
}
}
}

关于javascript - 我正在尝试将数组映射到另一个映射函数中我的代码未同步运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55549095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com