Unable to delete _id field from object after create using feathersjs - javascript

i want to modify my hook.data object in my node application after insertion of data. actually i'm not able to.
create: [function(hook, next) {
delete hook.data._id;
hook.data = { problem: hook.data }
postJson(hook.app.get('jsprintUrl'), hook.data)
.then(data =>{
hook.result = data;
next()
})
}]
result: still _id is exist
{
"_id": "59ca334e7bc4e06b140aadf9",
"algorithm": [
{
"name": "SA"
}
]
}

i update object using the hook.result in following way and hook.result will have its Mongoose documents converted to plain objects. more information reference link
create: [function(hook, next) {
delete hook.result._id;
hook.result = { problem: hook.result }
postJson(hook.app.get('jsprintUrl'), hook.result)
.then(data =>{
hook.result = data;
next()
})
}]
result: It was removed from response
{
"algorithm": [
{
"name": "SA"
}
]
}

Related

Trying to write a recursive asynchronous search in JavaScript

I am trying to write some code that searches through a bunch of objects in a MongoDB database. I want to pull the objects from the database by ID, then those objects have ID references. The program should be searching for a specific ID through this process, first getting object from id, then ids from the object.
async function objectFinder(ID1, ID2, depth, previousList = []) {
let route = []
if (ID1 == ID2) {
return [ID2]
} else {
previousList.push(ID1)
let obj1 = await findObjectByID(ID1)
let connectedID = obj1.connections.concat(obj1.inclusions) //creates array of both references to object and references from object
let mapPromises = connectedID.map(async (id) => {
return findID(id) //async function
})
let fulfilled = await Promise.allSettled(mapPromises)
let list = fulfilled.map((object) => {
return object.value.main, object.value.included
})
list = list.filter(id => !previousList.includes(id))
for (id of list) {
await objectFinder(id, ID2, depth - 1, previousList).then(result => {
route = [ID1].concat(result)
if (route[route.length - 1] == ID2) {
return route
}})
}
}
if (route[route.length - 1] == ID2) {
return route
}
}
I am not sure how to make it so that my code works like a tree search, with each object and ID being a node.
I didn't look too much into your code as I strongly believe in letting your database do the work for you if possible.
In this case Mongo has the $graphLookup aggregation stage, which allows recursive lookups. here is a quick example on how to use it:
db.collection.aggregate([
{
$match: {
_id: 1,
}
},
{
"$graphLookup": {
"from": "collection",
"startWith": "$inclusions",
"connectFromField": "inclusions",
"connectToField": "_id",
"as": "matches",
}
},
{
//the rest of the pipeline is just to restore the original structure you don't need this
$addFields: {
matches: {
"$concatArrays": [
[
{
_id: "$_id",
inclusions: "$inclusions"
}
],
"$matches"
]
}
}
},
{
$unwind: "$matches"
},
{
"$replaceRoot": {
"newRoot": "$matches"
}
}
])
Mongo Playground
If for whatever reason you want to keep this in code then I would take a look at your for loop:
for (id of list) {
await objectFinder(id, ID2, depth - 1, previousList).then(result => {
route = [ID1].concat(result);
if (route[route.length - 1] == ID2) {
return route;
}
});
}
Just from a quick glance I can tell you're executing this:
route = [ID1].concat(result);
Many times at the same level. Additional I could not understand your bottom return statements, I feel like there might be an issue there.

How to Iterate Data in vue.js

I am trying to iterate this data on vue inside the method function
{
"data": [
{
"id": 1,
"name": "Jack Daniels",
"mobile": "21223",
"start": "2021-02-25T09:16:21.000000Z",
"end": "2021-02-25T09:16:21.000000Z"
}
]
}
here is my code
async getEvents() {
try {
const response = await axios.get('/api/customers')
Object.entries(response).forEach(([key, value]) => {
console.log(`${key}:${value}`)
})
} catch (err) {
console.log(err)
}
}
and here is the output on
data:[object Object],[object Object]
any idea what went wrong and if there is better approach for iterating the data in vue.js
First of all it's not Vue question(axios/js tags).
You don't need Object.entries here (if you need to iterate data array).
Just use map for array and that's all.
const iteratedData = response.data.map((item, index) => {
console.log(`Index ${index}`);
console.log(item);
// Do what you need with data, your iteration
return item;
});
Code is fine. Here key is data and value is array of object. so to access value, here is a sample code
let response = {
"data":[{
"id":1,
"name":"Jack Daniels",
"mobile":"21223",
"start":"2021-02-25T09:16:21.000000Z",
"end":"2021-02-25T09:16:21.000000Z"
}]
};
Object.entries(response).forEach(([key,value])=>{
console.log(`${key}:${value[0].id}`)
})
Here value is an array so iterate value to get properties. It is just a sample to show logic.

Node.js: Cannot read property 'then' of undefined (nested query)

I'm trying to update a field in a MongoDB collection which has nested documents. I have to increase a certain value. The update query works just fine, but I need to nest it in another query where I get the current value, so I could increase it.
The nesting worked just fine when I used a faulty find() method. I realized I must use aggregate(). I can't get it working, the method returns undefined for some reason. I've tried the same aggregate query in the shell and it works, so it has to do something with the Node.js
The function that fails:
static addPointsToUser(mainId, userId, pointsToAdd) {
const db = getDb();
function getCurrent() {
db.collection('mycoll')
.aggregate([
{ $match: { _id: mainId } },
{ $unwind: '$userPoints' },
{ $match: { 'userPoints._id:': userId } },
{ $group: { _id: 'userPoints._id', userPoints: { $push: '$userPoints.points' } } }
])
}
function updateNew(newPoints) {
db.collection('mycoll')
.updateOne(
{ _id: mainId },
{ $set: { "userPoints.$[elem].points": newPoints } },
{
multi: true,
arrayFilters: [{ "elem._id": userId }]
}
)
}
return getCurrent()
.then(result => {
console.log(result);
const newPoints = result.userPoints[0];
return updateNew(newPoints)
.then(result => {
console.log(result);
return result;
})
})
}
The document looks like this:
{
"_id": ObjectId("5d4048f56a895612acabe0a9"),
// Some other fields
"userPoints": [
{ "_id": "manualID1", "points": 80 },
{ "_id": "manualID2", "points": 90 }
]
}
Expected aggregate result:
{ "_id" : "manualID1", "userPoints" : [ 90 ] }
Mongo shell gives the result seen above.
Actual result:
TypeError: Cannot read property 'then' of undefined
If I log the aggregate result it prints and empty array ( [] ).
Your methods getCurrent and updateNew are not returning anything. Which mean you are using .then() on something which is undefined as stated by your error message.
Adding a return statement before db.collection('mycoll') should help you with that.

I want to remove the property inside a object in mongodb

I have this bson doc in a collection. I want to remove a key based on dynamic value.
{
"_id": ObjectId("53ccff9bbb25567911f208a2"),
"image": {
"image1": "5213423424234.jpg",
"image2": "5213423424235.jpg",
"image3": "5213423424236.jpg"
}
}
In request I will get "image1"
temp/5bb3685b663c6f001d14c5da/dl/image1
I saved the key in a variable
let keyid = req.params.k_id
If I call the key directly, this works.
let qry = {
_id: mongojs.ObjectId(req.params.p_id)
}
let update = {
$unset: {
"image.image1": ""
}
}
db.inventory.findAndModify({
query: qry,
update: update,
safe: true
}, function (err, doc) {
if (err) {
res.status(500).json({
"success": false,
"error": err
})
return
}
res.status(200).json({
"success": true,
"message": "Deleted image key"
})
return
})
But since the key is dynamic, I am not able to find the solution with various possibilities
// Try1
$unset: {
'image.' + keyid: ""
},
// Try2
$unset: {
`image.${keyid}`: ""
}
You can try to change the update obj like this
let update = {}
update["$unset"] = {};
update["$unset"]['image.' + keyid] = '';
You need to build up your $unset object programmatically:
This question was answered here: using a variable in mongodb update

How to access nested array inside a JSON object from javascript

I am working with a json object that has nested arrays as well as names with spaces such as Account ID. I need to display just the Account ID's in my Vue.js application. I am able to get my entire response.data json object but not too sure how to get just the Account ID when it's nested like the example below.
JSON
"response": {
"result": {
"Accounts": {
"row": [
{
"no": "1",
"FL": [
{
"val": "ACCOUNT ID",
"content": "123456789"
},
...
Vue.js
<script>
import axios from "axios";
export default {
name: 'HelloWorld',
data () {
return {
accounts: [],
accountIDs: []
}
},
mounted() {
var self = this;
axios.get('https://MYAPIGETREQUEST')
.then( function(res){
self.accounts = res.data;
self.accountIDs = //This is where I want to get the Account ID
console.log('Data: ', res.data);
})
.catch( function(error){
console.log('Error: ', error);
})
}
}
</script>
Try something like this
if(res.data.response.result.Accounts.row[0].FL[0].val === 'ACCOUNT ID') {
self.accountIDs = res.data.response.result.Accounts.row[0].FL[0].content;
...
}
You can also try something like this:
let rowAccounts = response.result.Accounts.row
.map(row => row.FL
.filter(FL => FL.val === 'ACCOUNT ID')
.map(acc => acc.content)
);
self.accountIDs = [].concat.apply([], rowAccounts);
In rowAccounts, you get and array of accounts array per row like:
[
0: ['acc row 1', 'another acc row1'],
1: ['acc row 2'....]
]
Now it all depends upon your implementation the way you like it.

Categories