如何从 Express.Router.post() 中获取 Mongoose.Schema 属性? (+发送 Firebase 通知)
How do I get a Mongoose.Schema property from inside a Express.Router.post()? (+send a Firebase notification)
我尝试使用此代码在 post 将数据发送到我的远程数据库时发送 Firebase 通知,使用硬编码注册令牌,它有效(通知 + 存储):
const mongoose = require('mongoose');
const Offer = require('../models/offer');
var admin = require("firebase-admin");
var payload = {
notification: {
title: "This is a Notification",
body: "This is the body of the notification message."
}
};
var options = {
priority: "high",
timeToLive: 60 * 60 *24
};
//exports to my Express.Router.post()
//which is in a different file : router.post('/', OffersController.offers_create_offer);
exports.offers_create_offer = (req, res, next) => {
const offer = new Offer({
_id : new mongoose.Types.ObjectId(),
userId : req.body.userId,
postId : req.body.postId,
pricingDetails : req.body.pricingDetails,
serviceDetails : req.body.serviceDetails
});
offer.save().then(result => {
//my hardcoded Registration Token
admin.messaging().sendToDevice("dfs...f49", payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
console.log(result);
res.status(201).json({
message : 'Offer created successfully',
offer : {
_id : result._id,
userId : result.userId,
postId : result.postId,
pricingDetails : result.pricingDetails,
serviceDetails : result.serviceDetails
}
})
})
.catch(err => {
console.log(err);
res.status(500).json({
error : err
});
});
};
现在我想从我的用户架构中获取令牌:
const mongoose = require('mongoose');
const userSchema = mongoose.Schema({
_id : mongoose.Schema.Types.ObjectId,
email : {
type : String,
required : true,
unique : true,
match : /[a-...])?/
},
password : {type : String, required : true},
name : {type : String, required : true},
FCMRegToken : {type : String, required : true},
});
module.exports = mongoose.model('User', userSchema);
这是我的 post body 以防万一:
{
"userId": "5...c4",
"postId": "5...2b",
"pricingDetails" : "pricing",
"serviceDetails" : "details",
"jobUserId" : "5...3f"//which is a different userId
}
我特别需要获取对应于 req.body.jobUserId 的 FCMRegToken,如下所示:
...
const Offer = require('../models/offer');
const User = require('../models/user');
...
exports.offers_create_offer = (req, res, next) => {
//trying to get the Token
user = User.findOne({ _id : req.body.jobUserId});
FCMRegToken = user.FCMRegToken;
...
offer.save().then(result => {
//trying to pass it as an argument
admin.messaging().sendToDevice(FCMRegToken , payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
但它没有用,我收到一条警告,我的 FCMRegToken“必须是一个 non-empty 字符串...”,我知道我检查了我的数据库并确保 Token where _id = jobUserId不为空且正确。
是否有实现此目标的正确方法?
在尝试了一些东西之后,我找到了一种方法......更像是我了解查询在 mongoose 中的工作方式,这是对我有用的方法:
...
const Offer = require('../models/offer');
const User = require('../models/user');
...
exports.offers_create_offer = (req, res, next) => {
...
offer.save().then(result => {
console.log(result);
res.status(201).json({
message : 'Offer created successfully',
offer : {
_id : result._id,
userId : result.userId,
postId : result.postId,
pricingDetails : result.pricingDetails,
serviceDetails : result.serviceDetails
}
})
//trying to get the corresponding user document
const user = User.findOne({ _id : req.body.postUserId});
user.select('_id FCMRegToken')
.exec()
.then(doc => {
//trying to pass the user doc's property FCMRegToken as an argument
admin.messaging().sendToDevice(doc.FCMRegToken , payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
})
})
...
我尝试使用此代码在 post 将数据发送到我的远程数据库时发送 Firebase 通知,使用硬编码注册令牌,它有效(通知 + 存储):
const mongoose = require('mongoose');
const Offer = require('../models/offer');
var admin = require("firebase-admin");
var payload = {
notification: {
title: "This is a Notification",
body: "This is the body of the notification message."
}
};
var options = {
priority: "high",
timeToLive: 60 * 60 *24
};
//exports to my Express.Router.post()
//which is in a different file : router.post('/', OffersController.offers_create_offer);
exports.offers_create_offer = (req, res, next) => {
const offer = new Offer({
_id : new mongoose.Types.ObjectId(),
userId : req.body.userId,
postId : req.body.postId,
pricingDetails : req.body.pricingDetails,
serviceDetails : req.body.serviceDetails
});
offer.save().then(result => {
//my hardcoded Registration Token
admin.messaging().sendToDevice("dfs...f49", payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
console.log(result);
res.status(201).json({
message : 'Offer created successfully',
offer : {
_id : result._id,
userId : result.userId,
postId : result.postId,
pricingDetails : result.pricingDetails,
serviceDetails : result.serviceDetails
}
})
})
.catch(err => {
console.log(err);
res.status(500).json({
error : err
});
});
};
现在我想从我的用户架构中获取令牌:
const mongoose = require('mongoose');
const userSchema = mongoose.Schema({
_id : mongoose.Schema.Types.ObjectId,
email : {
type : String,
required : true,
unique : true,
match : /[a-...])?/
},
password : {type : String, required : true},
name : {type : String, required : true},
FCMRegToken : {type : String, required : true},
});
module.exports = mongoose.model('User', userSchema);
这是我的 post body 以防万一:
{
"userId": "5...c4",
"postId": "5...2b",
"pricingDetails" : "pricing",
"serviceDetails" : "details",
"jobUserId" : "5...3f"//which is a different userId
}
我特别需要获取对应于 req.body.jobUserId 的 FCMRegToken,如下所示:
...
const Offer = require('../models/offer');
const User = require('../models/user');
...
exports.offers_create_offer = (req, res, next) => {
//trying to get the Token
user = User.findOne({ _id : req.body.jobUserId});
FCMRegToken = user.FCMRegToken;
...
offer.save().then(result => {
//trying to pass it as an argument
admin.messaging().sendToDevice(FCMRegToken , payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
但它没有用,我收到一条警告,我的 FCMRegToken“必须是一个 non-empty 字符串...”,我知道我检查了我的数据库并确保 Token where _id = jobUserId不为空且正确。
是否有实现此目标的正确方法?
在尝试了一些东西之后,我找到了一种方法......更像是我了解查询在 mongoose 中的工作方式,这是对我有用的方法:
...
const Offer = require('../models/offer');
const User = require('../models/user');
...
exports.offers_create_offer = (req, res, next) => {
...
offer.save().then(result => {
console.log(result);
res.status(201).json({
message : 'Offer created successfully',
offer : {
_id : result._id,
userId : result.userId,
postId : result.postId,
pricingDetails : result.pricingDetails,
serviceDetails : result.serviceDetails
}
})
//trying to get the corresponding user document
const user = User.findOne({ _id : req.body.postUserId});
user.select('_id FCMRegToken')
.exec()
.then(doc => {
//trying to pass the user doc's property FCMRegToken as an argument
admin.messaging().sendToDevice(doc.FCMRegToken , payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
})
})
...