Drupal 8 - find node by Uri - drupal

I'm not a Drupal developer but I have to fix a website that seems to be breaking because of a node, so I'm trying to figure out which node it is and why it's causing the error. So I changed the getRouteName method in the Url.php, in the core to this:
public function getRouteName() {
if ($this->unrouted) {
$x = Url::fromUri($this->getUri());
dd(var_dump($x));
die(' === THIS IS CAUSING THE ERROR');
throw new \UnexpectedValueException('External URLs do not have an internal route name.');
}
return $this->routeName;
}
Which gives me this:
object(Drupal\Core\Url)#3626 (12) { ["urlGenerator":protected]=> NULL ["urlAssembler":protected]=> NULL ["accessManager":protected]=> NULL ["routeName":protected]=> NULL ["routeParameters":protected]=> array(0) { } ["options":protected]=> array(0) { } ["external":protected]=> bool(false) ["unrouted":protected]=> bool(true) ["uri":protected]=> string(46) "base:node/2fd814b7-2c74-4aa7-8e28-95625e2f9b8c" ["internalPath":protected]=> NULL ["_serviceIds":protected]=> array(0) { } ["_entityStorages":protected]=> array(0) { } } === THIS IS CAUSING THE ERROR
Then I'm trying to find the node with this uuid: 2fd814b7-2c74-4aa7-8e28-95625e2f9b8c but it doesn't exist in the database. I'm looking in the whole database to try to find it but it seems not to exist.
I'm a bit lost. Can this debug information take me to the content that is causing the error?

To get node id of current page, simply use:
\Drupal::request()->get('nid')
And, best way to debug the breaking page is to see the logs.

Related

Unable to use array's method "find" in Vue 3

I am trying to get current location of a user and then push it into array. Before I do so, I check whether a city with the same name is already there. In that case, I won't push it into the array. However, when I am trying to check it, it says: Uncaught (in promise) TypeError: Cannot read properties of null (reading 'find').
const found = ref(false);
const weatherResponse = ref([]);
function getLocation() {
console.log("SETTING LOCATION");
navigator.geolocation.getCurrentPosition((position) => {
console.log(`Lat: ${position.coords.latitude}, Lon: ${position.coords.longitude}`);
if (position.coords.latitude && position.coords.longitude) {
axios.get(`https://api.weatherapi.com/v1/current.json?key=${API_KEY}&q=${Math.round(position.coords.latitude)},${Math.round(position.coords.longitude)}&aqi=no`)
.then((response) => {
found.value = weatherResponse.value.find((item) => item.location.name == response.data.location.name);
if (response.data?.error?.code != 1006 && !found.value) {
weatherResponse.value.push(response.data);
this.$store.commit("addToList", response.data);
console.log(weatherResponse.value);
}
})
}
},
(error) => {
console.log(error.message);
}
)
}
I've already tried using fetch, axios to grab the API, but the "find()" method is still not working. Regarding "found" variable, I tried using it in ref as well as declaring it as "let found".
After trying and testing, I've finally managed to get everything to work. My issue was in (weirdly) main.js. Because it was set out like this: createApp(App).use(cors, store).mount('#app') it, I guess, caused VueX.store not to load in properly because mounted hook was called and it was throwing all sorts of mistakes. Putting it like const app = createApp(App); app.use(store); app.use(cors); app.mount("#app"); actually made it work.

Firestore security rules Multiple get() not working as expected

These are my firestore security rules:
service cloud.firestore {
match /databases/{database}/documents {
match /collectionA/{someID} {
function checkA() {
return get(/databases/$(database)/documents/collectionA/$(someID)/users/$(request.auth.uid)).data.deleteFlag != true
}
function checkB() {
return get(/databases/$(database)/documents/collectionB/$(request.auth.uid)/companies/$(someID)).data.deleteFlag != true
}
allow read, write: if checkA() || checkB()
}
}
}
And this is my database objects:
project {
collectionA {
companyA {
users {
r9Myn4TfzAVpSZGzyaet {
deleteFlag: false
}
}
}
}
collectionB {
aAzUlfztdYdEIXT3Tva73kCiuy93 {
companies {
companyA{
deleteFlag:false
}
}
}
}
}
And I tried Simulator :
simulation type : get
location : collectionA/companyA
provider : password
Firebase UID : aAzUlfztdYdEIXT3Tva73kCiuy93
I expected this security rules return "true".
Because I thought checkA returned "false" and checkB returned "true".
But they returned "false".
Please let me know if you have any ideas or suggestions.
I heard that the simulator sometimes does not work properly.
I threw the query from the client and verified it and it worked correctly.
So I thought it was a bug in the simulator.
However, I received the following response from Developer Platform Support.
FYI
The get function will get an error if trying to get a value for an
object that does not exist. When an error occurs in the condition
judgment, the security rule is not applied in the security rule.
Because of the specifications of security rules, it is impossible to
control on nonexistent objects

Firebase: Get notified just after the firebase-function trigger promise completed

In my app I pushed some object to my firebase-database and immediately after that (after the then-promise fully filled) I fetch the object (with the returned key) from the database (with the on-value method).
In addition, I make some changes on the pushed object using the firebase-functions.
How can I receive the object (in the app) just after the changes and not before? (like other ordinary backend services)
I hope this helps you, I have not tested this piece of code but it should help you in the right direction.
Also dont use this exact code in production, there is plenty room for improvement, this is just an example code.
exports.testFunction = functions.https.onRequest((req, res) => {
if (req && req.body) {
if (
req.body.hasOwnProperty('name') &&
req.body.hasOwnProperty('age')
) {
const person = {
name: req.body['name'],
age: req.body['age']
}
// Make some changes to the person object
person['hobby'] = 'Programmer';
// Add object to FireStore
admin
.firestore()
.collection('/persons')
.add(person)
.then((success) => {
// Return the added & changed person
res.status(201).send(JSON.stringify(person));
})
.catch((error) => {
// Error
console.error('Something broke', error)
res.status(500).send();
});
}
else {
// Error
res.status(500).send({err: 'Missing property'});
}
}
else {
// Error
res.status(500).send({err: 'Missing something'});
}
});

Meteor template updates before result of Meteor.users.update

I'm trying to figure out how to prevent a template from updating until Meteor.users.update() finishes.
First I'm trying to make sense of the documentation and the use of an optional callback argument in order to sort out what is happening.
Here is what I have:
Meteor.users.update(Meteor.userId(),
{$set:{'profile.reviewList': []}},
[],
function(err, result){
if (err){
console.log('oh no!');
} else {
console.log('Result achieved: '+this.profile.reviewList);
}
});
Currently the console.log('Result achieved: '+this.profile.reviewList); always returns something like ...TypeError: Cannot read property 'reviewList' of undefined... the first time though which tells me its firing before the result comes back.
I'm sure I'm not implementing the callback properly, but I was attempting to model this answer: How do you ensure an update has finished in meteor before running a find?
I'd really just like to delay the re-rendering of the associated template until the property gets created.
Any help will be greatly appreciated.
You assume that scope (this) in callback function return user object, which is wrong.
If you want to get user object in that callback simply query it there:
var user = Meteor.users.find(Meteor.userId()).fetch()
Another thing, you passed empty array as 2nd argument which is not needed.
Meteor.users.update(
Meteor.userId(),
{
$set: {
'profile.reviewList': 'testData'
}
},
function(err, result) {
if (err) {
console.log('oh no!');
} else {
var user = Meteor.users.find(Meteor.userId()).fetch();
console.log('Result achieved: ' , user && user.profile && user.profile.reviewList);
}
}
);

How to display the errors in a meteor view template

How can I go about getting the value of the errors I have thrown in a fails insert on a collection or in a method.
Customers.allow({
insert: function(userID, rec) {
console.log(userID === rec.userID);
if (rec.userID === null) {
throw new Meteor.Error(600, "You must be logged in");
};
if (rec.phone.length != 10 ) {
throw new Meteor.Error(601, "Incorect phone format", "Phone must be 10 chars long");
};
if (rec.fax.length != 10 ) {
throw new Meteor.Error(602, "Incorect fax format", "Fax must be 10 chars long");
};
return userID === rec.userID;
}
});
So right now i see the error on the console but say if wanted this display the errors in the template or store it in a reactive session so it can be shown to the user to correct.
like try to to something like this.
Template.form.errors = function () {
// return however you get to those thrown errors
}
There was a package just released today to help with this : https://github.com/tmeasday/meteor-errors
You would need meteorite to use it : https://github.com/oortcloud/meteorite
Add the package with meteorite:
mrt add errors
(Don't worry you're not adding errors to your meteor besides the command ;)
You can then throw errors in your client js:
Meteor.Errors.throw("Error details");
Then wherever you want to display errors use, in your HTML:
{{>meteorErrors}}

Resources