'mongodb functions from node workers. Cannot read properties of undefined
Trying to save some data into mongodb via node workers. I tested my crud api and that wprks but when I make the call via node worker its coming up undefined.
I suspect that maybe it is something to do with it not knowing if this is a post or push request? but how may I resolve this?
console log the object being logged is the data im trying to save. This is just a test not my actual target data
here is my DAO function. If i use a val like {test: 1, test1: 2, test2: 3} and insert that it works via insomnia
static async post2Beauty(something) {
console.log({test: something.New_Zealand_dollar})
try {
return await beauty.insert(something);
} catch (err) {
console.log(err);
}
}
here is the controller function
static async apiPostSomething(req, res, next) {
let result
req ? result=req : console.log("Data did not persist properly")
try {
await BeautyDAO.post2Beauty(result); // make this dynamic.
} catch (err) {
console.log(err);
}
}
here is node worker
const BeautyCtrl = require('./api/beautyData.controller');
const { parentPort } = require('node:worker_threads');
try {
parentPort.once("message", (message) => {
console.log(`recieved data from main`)
console.log(message)
try {
BeautyCtrl.apiPostSomething(message)
} catch (error) {
console.log(error)
}
})
} catch (error) {
console.log(error)
}
Solution 1:[1]
axios.post('http://localhost:5000/api/v1/addItem', {
method: 'post',
data: message
})
.catch(e => {console.log(e)})
and then
return await beauty.insert(something.body.data);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tyler2P |