I can`t delete anything from my MongoDB [duplicate] - javascript

I'm currently working on my first node.js rest api with express, mongodb (atlas cloud) and mongoose, when i try to make a .remove request i get this error:
{
"error": {
"name": "MongoError",
"message": "Cannot use (or request) retryable writes with limit=0",
"driver": true,
"index": 0,
"code": 72,
"errmsg": "Cannot use (or request) retryable writes with limit=0"
}
This is my request:
router.delete('/:productId', (req, res, next) => {
const id = req.params.productId;
Product.remove({ _id: id })
.exec()
.then(result => {
res.status(200).json(result);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
})
}); ;
});

The findOneAndRemove() function would work more accordingly since its specific to the filtering method passed in the function .findOneAndRemove(filter, options) to remove the filtered object. Still, if the remove process is interrupted by the connection the retryRewrites=true will attempt the execution of the function when connected.
More information here
When using retryRewrites set to true tells the MongoDB to retry the same process again which in fact can help prevent failed connections to the database and operate correctly, so having it turn on is recommended.
More info here
If you are using Mongoose 5^ and MongoDB 3.6 your code is better written like:
mongoose.connect('mongodb.....mongodb.net/test?retryWrites=true', (err) => {
if(err){
console.log("Could not connect to MongoDB (DATA CENTER) ");
}else{
console.log("DATA CENTER - Connected")
}
});// CONNECTING TO MONGODB v. 3.6
router.delete('/:productId', (req, res, next) => {
const id = req.params.productId;
Product.findOneAndRemove({ _id: id })//updated function from .remove()
.exec()
.then(result => {
res.status(200).json({
message: "Product Removed Successfuly"
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
})
}); ;
});

I just changed the true to false in retryWrites=true and it worked. Is that a good approach? Or there is a better way to solve this problem?

retryWrites=true is a good thing, a workaround for this incompatibility is to use findOneAndRemove instead of remove (looks like you're using mongoose)

Related

How to sort NeDB database in get response? (Javascript callback/arrow function syntax confusion)

I have an express server and a simple NeDB database. I can successfully get the whole database like so:
app.get('/api', (request, response) => {
//queuery the database for everything
db
.find({}, (error, data) => {
if (error) {
response.end();
console.log(error)
return;
}
console.log(data)
response.json(data)
})
But I noticed the results are, for some reason, not the same order as the database file. I want to sort by one of the timestamps. The database looks like:
...
{"lat":1,"lon":7,"timestamp":1585781054239,"_id":"3cZvJfQyLEXK0SZo","createdAt":{"$$date":1585781054240},"updatedAt":{"$$date":1585781054240}}
{"lat":1,"lon":2,"timestamp":1585781047536,"_id":"DN9bpd1FygEowgtc","createdAt":{"$$date":1585781047538},"updatedAt":{"$$date":1585781047538}}
{"lat":1,"lon":6,"timestamp":1585781052398,"_id":"Dzp6x0xo3QM960Rm","createdAt":{"$$date":1585781052400},"updatedAt":{"$$date":1585781052400}}
{"lat":1,"lon":5,"timestamp":1585781051174,"_id":"KswtMYzV2QBE3xkb","createdAt":{"$$date":1585781051176},"updatedAt":{"$$date":1585781051176}}
...
I admittedly haven't quite wrapped my head around how the callbacks work in this code. I have tried something like the following but it returns a 500 GET error to the client and returns "TypeError: Cannot read property 'sort' of undefined" to the server:
app.get('/api', (request, response) => {
//queuery the database for everything
db
.find({}, (error, data) => {
if (error) {
response.end();
console.log(error)
return;
}
// console.log(data)
// response.json(data)
})
.sort({ createdAt: -1 }, (data) => {
console.log(data)
response.json(data)
});
});
I wonder if it should be nested in the .find() function but at this point I'm quite in over my head and I believe I'm just not understanding the syntax. I have found examples of sorting but not in this context.
You can write something like this to sort it via timestamp:
database.find({}).sort({"timestamp":-1}).exec(function(err, data) {
if (err) {
response.end();
return;
}
console.log(data);
});

How to pass information from Node to React

I have an object i need to parse to React.
I'm trying to get the "rows" object (in the node function) over to a React State.
The 2 piece of code below are on different pages!
The other issue is
GET http://localhost:3000/new net::ERR_CONNECTION_REFUSED
I am currently running these both locally
React http://localhost:3001/
Node - http://localhost:3000/
There have been SIMILAR questions to this but I can't find an answer with both issues!
Thanks
router.get("/new", (req, res) => {
let parentList = sql.fetchAllParents(function(err, rows) {
res.setHeader("Access-Control-Allow-Origin", "http://localhost:3001");
if (err) throw err;
res.render('new', {parents: rows});
});
});
componentDidMount() {
fetch(`http://localhost:3000/new`).then(response => {
console.log(response)
return response.json();
}).then(data => {
// Work with JSON data here
console.log(data,'data');
}).catch(err => {
// Do something for an error here
console.log("Error Reading data " + err);
});
}
Send response from node "new" api. Like this:
router.get("/new", (req, res) => {
......
......
res.send({status:200,parents:data})
});

How to add conditional checks over ORM (sequelizejs) queries?

Suppose i have a table gameData {gameId, currentBoardLayout}, a get request like www.chess.com/asd123 is sent over to the server, where asd123 is my game id, I need to catch this id (asd123 which is my gameId) and check for it in my table (gameData) and implement the following logic :
srv.get('/:id', (req, res) => {
if ( gameData.findAll({where: {gameId: req.params.id} )
{ // Game room found
return currentBoardLayout
}
else
{ error : Invalid game id }
})
How can I achieve this?
Thanks
You can use Sequelize's findById method.
srv.get('/:id', (req, res) => {
gameData.findById(req.params.id)
.then(result => {
res.send(result)
})
.catch(() => {
res.send({
error: 'Could not find ID'
})
})
})
Here's what is happening:
Sequelize's findById method will return a promise. If it successful, you will get your item from the database back. If the item cannot be found, the catch method will fire.
res.send is Express' way of sending data back to the client.
It is worth checking out the Sequelize docs:
Using models
Querying

Mongoose/Express Update Route Cast to Object ID issue

Working with Express and Mongoose, and I'm writing an Update route, and I seem to have hit a wall with this route. I've tried to find the object being requested via:
router.patch('/:insiderId', (req, res) => {
Insider.findById(req.params.insiderId)
.then(insider => {
insider = Object.assign({}, insider, req.body);
insider
.save()
.then(updated => {
res.json(updated);
})
.catch(err =>
res
.status(400)
.json({ error: 'error updating insider', originalError: err })
);
})
.catch(err => {
console.error(err);
res.status(400).json({
error: 'error finding insider to update.',
originalError: err
});
});
});
But I'm still given an error stating that Cast to ObjectId failed for value \"5b16d9e9119bef28908f49c\" at path \"_id\" for model \"insiders\"
I did a little reading, and thought that findById would automatically cast the objectid, but it doesn't appear to be doing that.
Is my code wrong?

error: syntax error at or near "$"

I am currently checking out Vue and am doing a little refactor on a personal project.
I am running into some problems with my API.
The two technologies involved are axios which I am using to send requests to my API, which talks to a postgres database using pg-promise.
The api call...
function add (entry, cb) {
const length = entry.content.length
entry.title = `${entry.content.substring(0, 32)}`
axios.post('/api/notes', entry).then(cb)
}
here, entry is and object { title, content, prio, status, context }
the pg-promise endpoint
export const createNote = (req, res, next) => {
db.none('insert into entries(title, content, prio, status, context)' +
'values( ${title}, ${content}, ${prio}, ${status}, ${context})',
req.body)
.then(() => {
res.status(200)
.json({
status: 'success',
message: 'Inserted one entry'
})
}).catch(err => next(err))
}
here, req.body is undefined
I don't know why I am getting undefined.
Does this error log help?
I was reading through the documentation at axios and could not seem to find anything wrong with my api call, figured I would post something here.
Thanks!
req.body It has the following structure [{.....}]
for pg-promise need {....}
Solution to the problem req.body[0]
export const createNote = (req, res, next) => {
db.none('insert into entries(title, content, prio, status, context)' +
'values( ${title}, ${content}, ${prio}, ${status}, ${context})',
req.body[0])
.then(() => {
res.status(200)
.json({
status: 'success',
message: 'Inserted one entry'
})
}).catch(err => next(err))
}

Categories

Resources