问题描述
我有一个有角度的前端运送表单,当我单击“提交”时,值到达后端,但没有到达userId。我的应用通过Cognito用户池进行身份验证,并且我获得了令牌。
但我似乎无法传递userId。
我为documentation添加了$context.authorizer.principalId
。我也尝试过$context.authorizer.claims.userId
,但问题相同。
shipping.service.ts
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Http,Headers,Response } from '@angular/http';
import { Subject,BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
import { AuthService } from '../../auth.service';
@Injectable()
export class ShippingService {
dataEdited = new BehaviorSubject<boolean>(false);
dataIsLoading = new BehaviorSubject<boolean>(false);
dataLoaded = new Subject<[]>();
dataLoadFailed = new Subject<boolean>();
userData;
constructor(private http: HttpClient,private authService: AuthService) {
}
onStoreData(data) {
this.dataLoadFailed.next(false);
this.dataIsLoading.next(true);
this.dataEdited.next(false);
this.userData = data;
console.log("DATA",data)
this.authService.getAuthenticatedUser().getSession((err,session) => {
console.log(session);
let headers = new HttpHeaders();
headers.append('Authorization',session.getIdToken().getJwtToken());
headers.append('Accept','application/json');
headers.append('Access-Control-Allow-Origin','*');
let options = { headers: headers,reportProgress: true };
if (err) {
return;
}
this.http.post('https://nx96w6r3oc.execute-api.us-east-1.amazonaws.com/dev/shipping',data,options)
.subscribe(
(result) => {
this.dataLoadFailed.next(false);
this.dataIsLoading.next(false);
this.dataEdited.next(true);
},(error) => {
this.dataIsLoading.next(false);
this.dataLoadFailed.next(true);
this.dataEdited.next(false);
}
);
});
}
}
aws lambda函数
const AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = (event,context,callback) => {
console.log(event)
const params = {
Item:{
"UserId": {
S: event.userId
},"FirstName": {
S: event.firstName
},"LastName": {
S: event.lastName
},"Address1": {
S: event.address1
},"Address2": {
S: event.address1
},"State": {
S: event.state
},"City": {
S: event.city
},"Zip": {
S: event.zip
}
},TableName: "shipping"
};
dynamodb.putItem(params,function(err,data) {
if (err) {
console.log(err)
callback(err);
}
else{
console.log(data)
callback(null,data);
}
});
};
更新:
到目前为止,我注意到的几件事是在lambda函数中输出console.log(context)
时身份未定义。
我还在上面的角度服务中做了一个console.log(session)来验证user_id是否存在
我非常困惑为什么userId在输出中显示为空白字符串userId: ''
。感谢您的帮助!