Reputation: 17940
I've created a free DB on MLab and I've set up the connection in node, but when i try to insert I get an error:
Error:not authorized on capina to execute command { insert: \"products\", documents: [ { _id: ObjectId('5b255d56a7be644ca873631e'), name: \"טארלט מלוח אישי\", priceNoVAT: 0.97, priceVAT: 1.05, quantityPerUnit: 150, unit: \"ארגז\", totalPrice: 130, __v: 0 } ], ordered: true }"
Here is the connections code:
//works fine, no error here
mongoose
.connect(
"mongodb://" +
process.env.MONGO_USERNAME +
process.env.MONGO_PWD +
"@ds161620.mlab.com:61620/" + process.env.DB_NAME
)
.catch(err => {
console.error(err);
});
Here is the model:
const mongoose = require("mongoose");
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
priceNoVAT: Number,
priceVAT: Number,
quantityPerUnit: Number,
unit: String,
totalPrice: Number
});
module.exports = mongoose.model("Product", productSchema);
and here is the test route I'm trying to call and get the error:
router.post("/", (req, res) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: "טארלט מלוח אישי",
priceNoVAT: 0.97,
priceVAT: 1.05,
quantityPerUnit: 150,
unit: "ארגז",
totalPrice: 130
});
product
.save()
.then(result => {
res.json({ message: "Product Saved" });
})
.catch(err => {
console.log(err);
res.json({ message: "Error:" + err.message });
});
});
The username/password are defined properly and when i log it, i see i get the correct values, so I'm really not sure what I'm doing wrong?!
Upvotes: 0
Views: 25
Reputation: 17940
Finally got it, it's a stupid mistake but yet, it can happen to someone else, so I'm posting the answer here:
I forgot to add ":" between the username and password, so:
"mongodb://" +
process.env.MONGO_USERNAME +
process.env.MONGO_PWD +
"@ds161620.mlab.com:61620/" + process.env.DB_NAME
should actually be:
"mongodb://" +
process.env.MONGO_USERNAME +
":" +
process.env.MONGO_PWD +
"@ds161620.mlab.com:61620/" + process.env.DB_NAME
Upvotes: 0