Ok so I am using multer to upload an image of an item that a user submits. However when the user deletes the item the image file is still on the server.
I have been trying to figure this out and I am not sure if its the way findByIdAndRemove() is deleting the user item before I am making the call to remove the image or what but I am getting the error saying that it cant find the file name of undefined. I am using a promise thinking it would allow me to do this but I am not sure of where else to go. I am new at this and learning as I go. Here is my delete route:
router.delete("/item/:id", middleware.isLoggedIn, (req, res) => {
Promise.all([
(Item.findByIdAndRemove(req.params.id),
fs.unlinkSync("./public/uploads/" + req.item.image))
])
.then(() => {
return res.render("products");
})
.catch(err => {
return console.log("err", err.stack);
});
Any suggestions?
Related
'''if (await permission()) {
Geolocation.getCurrentPosition(
async position => {
await fetch(
`https://api.opencagedata.com/geocode/v1/json?q=${position.coords.latitude}+${position.coords.longitude}&key=apikey&pretty=1`,
).then(result => {
console.log(result.data);
});
},
error => {
console.log(error.code, error.message);
},
);
}'''
How do i retrive the returned location from the response sent to me
The things it sends me seems like gibrish and have no clue what to do and i am building the app in react-native using tsx
All you need to do is console the response to make sure that you are receiving your response as expected after that you've multiple choices to extract the required data e.g. Destructuring or looping through the response (quite expensive + time taking) or you can use the filter method, in a nutshell, there are tons of way to get your required data from the response and it's just a matter of your taste(like whichever you prefer)
I'm visiting a page which is fetching data Asynchronously (multiple XHR requests), and then asserting if a certain DOM element is visible/exists in the page.
So far I was only able to get the page and the data fetched with using cy.wait() either with an arbitrary time, or by aliasing the actual request, and using the promise-like syntax to make sure my cy.get() is done after the XHR response has completed.
Here is what doesn't work:
before(() => {
cy.login();
cy.server();
cy.route('/v0/real-properties/*').as('getRealPropertyDetails');
cy.visit('/real-properties/1/real-property-units-table');
});
beforeEach(() => {
Cypress.Cookies.preserveOnce('platform_session');
});
after(() => {
cy.clearCookies();
});
context('when viewport is below 1367', () => {
it('should be closed by default', () => {
cy.wait('#getRealPropertyDetails'); // the documentation says this is the way to go
sSizes.forEach((size) => {
cy.viewport(size[0], size[1]);
cy.get('.v-navigation-drawer--open.real-property-details-sidebar').should('not.exist');
});
});
Adding cy.wait(1000); in the before() or beforeEach() hooks also works, but this is not really an acceptable solution.
What works, but not sure if this is the way to do this (I would have to add this for every page, would be quite annoying) :
it('should be closed by default', () => {
cy.wait('#getRealPropertyDetails').then(() => {
sSizes.forEach((size) => {
cy.viewport(size[0], size[1]);
cy.get('.real-property-details-sidebar').should('not.be.visible');
});
});
});
I see that you have browser reloads there (beforeEach), which could potentially wipe out the route spy, but not sure why cy.wait().then would work. I would try switching from before to beforeEach though, creating things once is always trickier than letting them be created before each test
I am trying to make an api call using fetch method in react-native.
I have rn-fetch-blob for uploading images in one screen which is on another different screen. This particular function was for me to initiate a payment online using paystack. though the issue is not about the paystack because this worked in a new project without having rn-fetch-blob in it.
Secondly if i remove response.json(), it wouldn't return anything but there wouldn't be any error. but with response.json(), if get this error
attempt to invoke interface method 'java.lang.bridge.readablemap. getstring(java.lang.string)' on a null object reference.
I suspect there is a conflict between rn-fetch-blob and fetch because even without using rn-fetch-blob, it runs through perfectly, if i do
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
})
same error comes
i expect a reference value like hyhjgbhgfh
attempt to invoke interface method 'java.lang.bridge.readablemap. getstring(java.lang.string)' on a null object reference.
I logged out response and i saw
I know there are similar questions to this but i've tried the solutions and nothing seems to be working
My code takes user input, makes a axios get request based off that input and aims to assign the data to the array staffs[]
export default {
data(){
return{
staffs: [],
userInput:'',
errors: '',
}
},
created:function(){
bus.$on('search-data',inputData =>{
axios.get('http://localhost:4000/stafftest?username='+inputData)
.then(response => {
console.log(JSON.stringify(response.data))
this.staffs = response.data.data
})
.catch(error =>{
console.log(error)
})
})
},
}
Similar issue to what everyone else has asked, the axios request is made successfully but it won't assign data to the array
Here's a combo of things i tried:
https://codeshare.io/ammpP4 https://codeshare.io/G798xD
https://codeshare.io/2Ewk1P https://codeshare.io/GqDPPk
https://codeshare.io/GLbwwp
tried changing it to function(response) etc and having a var self=this, yet none of it seems to work
I even set up another test page which queries a pokemon API and the data assigmemt went smoothly, which leaves me confused as to why in this case on this certain page it won't work
Appreciate any ideas to get this fixed
Thanks
In Sails.js, one could receive an uploaded file as such:
myControllerAction: function(req, res) {
req.file('avatar', function(err, uploadedFiles) {
// uploaded avatar image will be available here
console.log(uploadedFiles[0]);
}
}
Suppose I received a file, but it is not properly formatted the way I want. I would just reply with an error. One thing I would like to do is make sure that received file does not remain in the filesystem (i.e. if it exists somewhere, delete it). How can I ensure that?
Just use node fs module to delete uploaded file.
const fs = require('fs');
fs.unlink(insertFilePathHere, function(err) {
if (err) return console.log(err); // handle error as you wish
// file deleted... continue your logic
});