我想创建一个迁移脚本,该脚本能够从Project const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const cors = require('cors')({
origin: true
});
const express = require('express');
const cookieParser = require('cookie-parser')();
const app = express();
// Express middleware that validates Firebase ID Tokens passed in the Authorization HTTP header.
// The Firebase ID token needs to be passed as a Bearer token in the Authorization HTTP header like this:
// `Authorization: Bearer <Firebase ID Token>`.
// when decoded successfully, the ID Token content will be added as `req.user`.
const validateFirebaseIdToken = (req, res, next) => {
console.log('Check if request is authorized with Firebase ID token');
if (
!req.headers.authorization ||
!req.headers.authorization.startsWith('Bearer ')
) {
console.error(
'No Firebase ID token was passed as a Bearer token in the Authorization header.',
'Make sure you authorize your request by providing the following HTTP header:',
'Authorization: Bearer <Firebase ID Token>'
);
res.status(403).send('Unauthorized');
return;
}
let idToken;
if (
req.headers.authorization &&
req.headers.authorization.startsWith('Bearer ')
) {
console.log('Found "Authorization" header');
// Read the ID Token from the Authorization header.
idToken = req.headers.authorization.split('Bearer ')[1];
console.log(idToken);
} else {
// No cookie
res.status(403).send('Unauthorized');
return;
}
admin
.auth()
.verifyIdToken(idToken)
.then(decodedIdToken => {
console.log('ID Token correctly decoded', decodedIdToken);
req.user = decodedIdToken;
return next();
})
.catch(error => {
console.error('Error while verifying Firebase ID token:', error);
res.status(403).send('Unauthorized');
});
};
app.use(cors);
app.use(cookieParser);
app.use(validateFirebaseIdToken);
app.get('/', (req, res) => {
res.send(`Your email is ${req.user.email}`);
});
// This HTTPS endpoint can only be accessed by your Firebase Users.
// Requests need to be authorized by providing an `Authorization` HTTP header
// with value `Bearer <Firebase ID Token>`.
exports.securedHttp = functions.https.onRequest(app);
字段复制到Project assignedTo
,但插入到包含用户详细信息的数组中,并且assignedMultiple
字段将为空。由于我们将使用assignedTo
,因此assignedTo
字段将被弃用。
这是我的assignedMultiple
表:
用户:
users
项目:
{
"username" : "usertest",
"firstName" : "Stack",
"lastName" : "Overflow",
"company" : "",
"country" : "",
"id" : ObjectId("this_is_usertest_id")
}
预期输出:
{
"name" : "Project",
"assignedTo" : ObjectId("this_is_usertest_id"),
"assignedMultiple" : [],
"id" : ObjectId("someid")
}
这是我到目前为止尝试过的,但是什么也没有发生,但给我一个错误:{
"name" : "Project",
"assignedTo" : null,
"assignedMultiple" : [{
"username" : "usertest",
"firstName" : "Stack",
"lastName" : "Overflow",
"company" : "",
"country" : "",
"id": ObjectId("this_is_usertest_id")
}]
"id" : ObjectId("someid")
}
,但是当我尝试使用failed to execute a script. TypeError: db.user.findOne(...) is null
时,它会打印带有数据的项目。
此链接基于此链接MongoDB : how to set a new field equal to the value of another field, for every document in a collection
print(project)
提前谢谢!