MongoDB document deleted locally but not on MongoLab - mongodb

This Mongoose delete method seems to work Ok locally with HttpRequester
router.delete('/', (req, res) => {
Book.findOneAndRemove({ title: req.body.title })
.then(() => res.json({ 'book deleted': 'success' }))
.catch(err => console.log('Couldn\'t delete book:', err));
}
);
but the MongoLab collection still shows the document. How to get it deleted remotely too? findOneAndDelete() didn't make a difference.
The complete repo is on https://github.com/ElAnonimo/booklist

findOneAndRemove had issues earlier.
findByIdAndRemove works fine.
router.delete('/', (req, res) => {
Book.findOne({ title: req.body.title })
.then((doc) => if(doc)return Book.findByIdAndRemove(doc._id))
.then(() => res.json({ 'book deleted': 'success' }))
.catch(err => console.log('Couldn\'t delete book:', err));
}
);
or even better you can do as follows
router.delete('/', (req, res) => {
Book.deleteOne({ title: req.body.title })
.then(() => res.json({ 'book deleted': 'success' }))
.catch(err => console.log('Couldn\'t delete book:', err));
}
);

Have you change your URI connection to MongoDB on mLab?. I think you've changed it yet.
Please sure use mongodb://<dbuser>:<dbpassword>#ds12xxxx.mlab.com:27342/[database_name], not locally 'mongodb://localhost/[database_name]'
If you've changed it, please use deleteOne https://mongoosejs.com/docs/api.html#model_Model.deleteOne, it's working well.

Related

Fetch response not logging to Console

Im attempting to fetch from my localhost URL but the all the console.log() except for "done" do nothign and my .catch isnt receiving an error
fetch(tURL, {
method: 'POST',
body: JSON.stringify(post),
mode: 'cors',
headers:{
'content-type': 'application/json'
}
}).then(res => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
form.reset();
console.log("Response received from server");
console.log(res.json);
return res.json();
})
.then(npost => {
console.log("Parsing response as JSON...");
form.style.display = '';
console.log(npost);
})
.catch(error => console.error(error));
console.log('done');
})
I put several debugging logs to see what was being reached but i get nothing
It looks like this is because of a syntax error, the }) at the end of your file should not be there, I think it is because the formatting is fairly difficult to read. If you write a .then statement between catch and console.log it will work fine. If you count the parenthesis between the fetch and the end, there is an extra set to close a block that doesn't exist. If you delete the }) at the end, your code will work, however the log statement will run before the fetch is complete; which I assume you don't want. To solve this, delete that }) and add this to the end of the chain:
.then(() => console.log('done'))
Original code
fetch(tURL, {
method: 'POST',
body: JSON.stringify(post),
mode: 'cors',
headers:{
'content-type': 'application/json'
}
}).then(res => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
form.reset();
console.log("Response received from server");
console.log(res.json);
return res.json();
})
.then(npost => {
console.log("Parsing response as JSON...");
form.style.display = '';
console.log(npost);
})
.catch(error => console.error(error));
// This is where the error is, the catch statement was the last "block" in the callback chain, so the `})` is closing a "block" that doesn't exist.
// Delete the semicolon above and uncomment the next line to fix
// .then(() => {
console.log('done');
})

PayPal Smart Payment Buttons - Alternative Payment Methods - 400 Bad Request

I'm using the PayPal Smart Payment Buttons on my website to capture payments.
The Order is created server-side via the PayPal-checkout-sdk for PHP.
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = self::buildRequestBody($paymentAC);
// 3. Call PayPal to set up a transaction
$client = PayPalClient::client();
$response = $client->execute($request);
The request body looks like this:
return array(
'intent' => 'CAPTURE',
'commit' => true,
'application_context' =>
array(
'brand_name' => 'EXAMPLE INC',
'locale' => 'de-DE'
'shipping_preference' => 'NO_SHIPPING'
),
'purchase_units' =>
array(
0 =>
array(
'reference_id' => $rId,
'description' => 'Ihr kauf bei EXAMPLE INC',
'invoice_id' => $paymentAC,
'amount' =>
array(
'currency_code' => $currency, #EUR
'value' => $price,
'breakdown' =>
array(
'item_total' =>
array(
'currency_code' => $currency,
'value' => $itemTotal,
),
'tax_total' =>
array(
'currency_code' => $currency,
'value' => 0,
),
),
),
'items' => $items,
),
),
);
The request works fine and an order is created. The script returns a JSON Object with the "id" key set to the ID of the created order.
{"id": "PAYMENTID"}
The script is called by the payment button like this:
paypal.Buttons({
createOrder: function() {
return fetch('./CreateOrder.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: '{"payment_ac": "<?php echo $_GET['code'] ?>"}'
}).then(function(res) {
console.log(res);
return res.json();
}).then(function(data) {
return data.id; // Use the key sent by your server's response, ex. 'id' or 'token'
});
},
onError: (err) => {
console.error('error from the onError callback', err);
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
$("#loadMe").modal({
backdrop: "static", //remove ability to close modal with click
keyboard: false, //remove option to close with keyboard
show: true //Display loader!
});
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
console.log(details);
//window.location.href = "verify.php?id=" + data.orderID + "&ac=<?php echo $internalID ?>";
$.get({
url: "/payment/verify.php?id=" + data.orderID + "&ac=<?php echo $internalID ?>",
success: function (respdata) {
if (respdata['payment'] === "completed") {
window.location.href = '/success/<?php echo $internalID?>&cid=' + respdata['cid'];
} else {
alert(respdata);
}
}
})
});
},
onCancel: function (data) {
console.log(data);
alert(data);
// Show a cancel page, or return to cart
window.location.href = "/payment/cancelled.php?ac=<?php echo $internalID ?>&oid=" + data.orderID;
},
style: {
color: 'black',
label: 'pay',
layout: 'vertical'
}
}).render('#paypal-button-container');
The payment buttons can open the checkout window successfully.
When I'm using sandbox credentials everything works fine. (PayPal, Simulated Alternative Payments & Credit Card payments)
When I'm switching to live credentials only PayPal payments seem to work. Every alternative payment method redirects to https://www.paypal.com/latinumcheckout/error/generic-error after you enter your details on https://www.paypal.com/latinumcheckout/change-userinfo
When in the developer console there is an error output:
the server responded with a status of 400 () app.bundle.js:41:140543
Credit card payments seem to fail as well.
I'm clueless, I spent a long time reading the documentation but have found no solution.

Cancel streamed postgres query in knex.js

Is there a way to cancel a streamed, long running query in knex.js?
Tried stream.emit('close') but it doesn't seem to close the query, as a knex.destroy() call never resolves.
const query = knex('tablename')
const stream = query.stream({ batchSize: 2000 })
process.on('SIGINT', () => stream.emit('close', 128 + 2))
process.on('SIGTERM', () => stream.emit('close', 128 + 15))
stream
.on('data', onData)
.on('close', (exitCode = 0) => {
knex.destroy()
.then(() => console.log('This is never resolved if the query has finished'))
.catch(err => console.error('could not close connection gracefully', err))
console.log('Finished')
process.exitCode = exitCode
})
.on('end', () => {
console.log('End')
stream.emit('close')
})

Mocha MongoDB clean collection before each test

I'm trying to cleanup 2 collections before each test. I'm using mocha --watch to rerun tests while editing the test source files. First run always executes as expected, but consecutive runs gives Topology was destroyed error from mongodb(indicated via result of http request).
I am not really sure why deleteMany deletes my inserted object in consecutive runs.
describe('myCollection1 related tests', () => {
// myCollection1 documents should refer to a valid myCollection2 document.
var foo;
const exampleObject = {name: 'TEST OBJECT', attr1: 'TO'};
beforeEach(() => {
return Promise.all([
mongo.db('mydb').collection('myCollection1').deleteMany({}), // clear collection 1
mongo.db('mydb').collection('myCollection2').deleteMany({}) // clear collection 2
.then(() => mongo.db('mydb').collection('myCollection2').insertOne(exampleObject) // and add a sample object
.then((value) => {
foo = value.ops[0]; // save this as test specific variable so I can use it in my tests.
return Promise.resolve();
})),
]);
});
it('should create a related object', (done) => {
chai.request(server)
.post('/api/v1/foos/')
.send({ related: foo._id })
.then((res) => {
res.should.have.status(200);
res.body.should.be.an('object').with.all.keys('status', 'errors', 'data');
done();
}).catch((err) => {
done(err);
});
});
});
I spotted issue with your promise structure in beforeEach. I'm not sure it is intended or not. I'm afraid it is the culprit. I'm fixing that into below:
beforeEach(() => {
return Promise.all([
mongo.db('mydb').collection('myCollection1').deleteMany({}),
mongo.db('mydb').collection('myCollection2').deleteMany({})
]) // close the promise.all here
.then(() => collections.engines().insertOne(exampleObject)) // close `then` here
.then((value) => {
foo = value.ops[0];
return Promise.resolve();
});
});
Hope it helps

the best way to exclude some data from a method in sails controller

I want's to exclude some data in some controller method and in other method i want's that data. I do it with forEach function right into method after finding that :
nine: function (req, res) {
Dore.find()
.limit(9)
.sort('createdAt DESC')
.populate('file')
.exec(function (err, sh) {
if (err) {
return res.negotiate(err);
} else {
console.log('before : ', sh);
sh.forEach(function (item, i) {
delete item.zaman;
});
console.log('after : ', sh);
return res.send(sh);
}
});
},
I want to know how possible to do that with finding and do not included ever in finding so we don't need to remove that again with forEach.
tanks
As #zabware say we have select method in Query option I try this format but do not work and return all data :
I try to use that with following format but don't working :
Model.find( {
where: {},
limit: 9,
sort: 'createdAt DESC'
},
{
select: [ 'id', 'createdAt' ]
} )
and
Model.find( {
where: {},
limit: 9,
sort: 'createdAt DESC',
select: [ 'id', 'createdAt' ]
} )
and
Model.find( {}, select: [ 'id', 'createdAt' ] )
Although toJson is really designed to solve the kind of problem you are facing, it will not help you, since in some actions you want to exclude certain fields and in others not.
So we need to select fields per query. Luckily there is a solution for this in waterline:
Dore.find({}, {select: ['foo', 'bar']})
.limit(9)
.sort('createdAt DESC')
.populate('file')
.exec(function (err, sh) {
console.log(err);
console.log(sh);
});
This is supported since sails 0.11 but not really documented. You can find it under query options here https://github.com/balderdashy/waterline-docs/blob/e7b9ecf2510646b7de69663f709175a186da10d5/queries/query-language.md#query-options
I accidentally stumbled upon it while playing with the debugger - Sails version 1.2.3
There is something like omit and You can put it into your find method call:
const user = await User.findOne({
where: {email : inputs.email},
omit: ['password']
}); //There won't be password field in the user object
It is a pity that there is no word about it in the documentation.