Error put array in firebase with clouds functions [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I got this error when I created api for update but it has array data in it. i am using cloud functions in firebase cli
i use the code below
app.put("/api/update/blog/:id", (req, res) => {
(async () => {
try {
const reqDoc = db.collection("blogdetails").doc(req.params.id);
await reqDoc.update({
title : req.body.title,
author : req.body.author,
kategori : req.body.kategori,
paragraf : req.body.paragraf,
image : req.body.image,
});
return res.status(200).send({ status: "Success", msg: "Data Updated" });
} catch (error) {
console.log(error);
res.status(500).send({ status: "Failed", msg: error });
}
})();
});

Related

Is there any way to display data from Firebase Firestore as a React Table? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
Is it possible to display data from Firebase Firestore in a React Table?
I tried several approaches, but none worked. I tried map function and listing them by creating a custom React element by providing the data using props.
Expectation:
My Expectation
I want to display the user email, UID, and password as a table.
My Code:
let data = [];
// creating an user
const createUser = () => {
let email = prompt("Enter the user's email: ");
let password = prompt("Enter the user's password: ");
let collectionName = `${window.localStorage.getItem("user-email")}_auth-details#BESTCLOUD`;
password = MD5(password);
db.collection(collectionName).add({
email: email,
password: password
}).then(function (docRef) {
alert("Successfully created!")
console.log("UID", docRef.id);
db.collection(`${window.localStorage.getItem("user-email")}_auth-details#BESTCLOUD`).onSnapshot((snapshot) => {
let x = snapshot.docs.map((doc) => ({
id: doc.id,
data: doc.data(),
}))
for (let i = 0; i < 5; i++) {
data.push({
email: x[i].data.email,
id: x[i].id,
num: i
});
console.log(data)
}
});
})
.catch(function (error) {
console.error("Error: ", error);
});
};
If I do this, data is
[
{
"id": "fMrLCDCkUqsV8BcoFcNY",
"data": {
"email": "peter123#gmail.com",
"password": "e7e761c4d399a441c6469a7c0626ac2d"
}
}
]

discord.js channel only command [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
Kepps crashing
const Discord = require("discord.js")
module.exports = {
name: 'not-dropping',
description: 'sets the dropping status!',
if (message.channel.id === '1059798572855476245') {
execute(message, args) {
message.delete(1000);
const name = ("dropping-🔴")
message.channel.setName(name)
message.channel.send(`Successfully set the dropping status to **${name}**`)
}
}
}
I also tried to change it to Role only but it contiunes crashing.
Having an if-statement in the definition of your export won't work. Instead, call the if-statement only when execute() is run, like this:
const Discord = require("discord.js")
module.exports = {
name: 'not-dropping',
description: 'sets the dropping status!',
execute(message, args) {
if (message.channel.id === '1059798572855476245') {
message.delete(1000);
const name = ("dropping-🔴")
message.channel.setName(name)
message.channel.send(`Successfully set the dropping status to **${name}**`)
}
}
}

Why im getting this error invalid Payload? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
import admin from "firebase-admin";
async function notifyUser(message ,title, photoURL,token , uid , notificationsOn ) {
const messages = [];
const payload = {
notification: {
title: title,
body: message,
},
data: {
uid,
},
android: {
notification: {
image: photoURL,
},
},
apns: {
payload: {
aps: {
"mutable-content": 1,
},
},
fcm_options: {
image: photoURL,
},
},
};
try {
if (token) {
messages.push({ token: token, ...payload });
console.log("yes");
}
if (messages.length && notificationsOn==true) {
await admin.messaging().sendAll(messages);
console.log("Token for user, send notification.");
}
} catch (error) {
const functions = require("firebase-functions");
functions.logger.log("Hello from info. Here's an object:", error);
console.log(photoURL);
console.log(notificationsOn);
console.log(token);
console.log(message);
console.log(uid);
console.log(title)
console.log("No token for user, can not send notification.");
return {error:error.code};
}
return;
};
export {notifyUser} ;
The error I get
The line 169 is this line
await admin.messaging().sendAll(messages);
It's always logging "No token for user cannot send notification". And I don't have any idea why. I checked like 30 times. The logs are not empty and give the correct output. The token is not empty.
It's not working on both android and iOS. But when sending notification via firebase to android or iOS it works. And notification retrieved .
Hope really anyone has and idea. I also tried to send notification from firebase via token. This also works . And as I said I checked every output it should work but does not.
If you think I did something wrong in configuration in my project please tell me what I you think it can be .
comes out that adding this statement
token:token like this
const payload = {
notification: {
title: title,
body: message,
},
token: token,
data: {
uid,
},
android: {
notification: {
image: photoURL,
},
},
apns: {
payload: {
aps: {
"mutable-content": 1,
},
},
fcm_options: {
image: photoURL,
},
},
};
into payload but outside of all other statements fixed the issue.

How to solve nested async database calls in Firebase Functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to get a list of objects and for each object there is a record in another collection which has to be called also. Both requests return promises and my question is, how to "wait" for the inner promises to be fulfilled before the next outer promises are done.
This is the code:
export async function getAllCompanies(req: Request, res: Response) {
try {
const listCompanies = await db.collection('companies').get()
const companies = listCompanies.docs.map(async doc => {
const data = doc.data()
const location = await db.collection('locations').doc(data.locationId).get()
const locationData = location.data()
return {
id: doc.id,
name: data.name,
address: locationData ? locationData.address : null,
zipCode: locationData ? locationData.zipCode : null,
city: locationData ? locationData.city : null,
country: locationData ? locationData.country : null,
email: data.email,
phoneNumber: data.phoneNumber,
type: data.type,
createTime: doc.createTime.toDate(),
lastUpdateTime: data.lastUpdateTime
}
})
return res.status(200).send({ companies })
} catch (err) {
return handleError(res, err)
}
}
I think you're looking for:
const resolvedCompanies = await Promise.all(companies);
return res.status(200).send({companies: resolvedCompanies});
just before the return res.status(...). This will wait for all of the promises you map-ed into the list of companies to resolve before returning and return the values instead of the promises.

Sending a json response in node js without status [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to send a json response to a GET call from node js server.
I am creating a json as follows
var request = require('request');
var parseString = require('xml2js').parseString;
env.server.get('localhost:3000/details', function(req, res) {
if (req.query.code) {
request.get({
url: 'http://www.example.com/net2/getDetails?code=' + req.query.code
}, function(err, result, body) {
if (err) {
console.log('error :' + err);
res.status(404).json({
infoMsg: "Error"
});
} else if (result.statusCode == 200) {
var resultBody = {};
parseString(body, function(e, parsedResult) {
resultBody["userName"] = parsedResult.userName;
resultBody["userDept"] = parsedResult.department;
resultBody["userCat"] = parsedResult.category;
res.send(resultBody);
});
}
})
} else {
console.log('Request failed .');
}
});
I am expecting a result which needs to be in this format
{
"name": "test",
"dept": "dept1",
"cat": "cat2"
}
But the response which I am getting is in this format
{
"status": "success",
"data": {
"name": "test",
"dept": "dept1",
"cat": "cat2"
}
}
How to create a response which is in the expected format ie without status and data being added, where am I going wrong.
If you're requesting the JSON with Ajax Request, then the status field is a primordial part of the 'res' and can't be removed.

Categories