I want to insert an data in DB , the problem is when I loop array I want to check if it has same username I want to push data into that object in DB else I want to create new, I try to use Promise all , reduce , It's run into parallel but It's work when I use for await loop but the eslint said no restricted synctax is thier anyway to fix it into eslint format
here is my example data
const data = [
{
username: "11"
code:["22"]
},
{
username: "11"
code:["23"]
}
]
what I expect data in DB is
const dataInDB = [
{
username: "11"
code:["22","23"]
},
]
Here is what it work with for loop but eslint show error
for await (const item of data) {
const findUser = await User.findOne({ username: item.username });
if (findUser == null) {
// add new data
} else {
// push data code into findUser
}
Here is what I try to use Promise all but It seems like the data add in parallel it didn't go into if else condition [ eslint didn't show up error in this way ]
await Promise.all(data.map(async (item) => {
const findUser = await User.findOne({ username: item.username });
if (findUser == null) {
//add into db
} else {
//push data
}
}));
here is my eslint config
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"extends": [
"airbnb-base"
],
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
"no-param-reassign": ["error", { "props": false }],
"no-underscore-dangle": "off"
},
"globals": {
"__basedir": "writable"
}
}