是的,这是因为 nextjs 构建了同构应用程序。所以尝试创建 webhook 并触发 mongodb 缝合 api,或者直接使用 webhook 中的服务。这是通过stitch webhook注册使用的例子:
exports = function(payload) {
const request = EJSON.parse(payload.body.text());
const http = context.services.get("<app-title>");
const owner = context.values.get("<username-of-the-app-owner>");
const apiKey = context.values.get("<api-key>");
return http.post({
url: "https://stitch.mongodb.com/api/admin/v3.0/auth/providers/mongodb-cloud/login",
body: JSON.stringify({
username: owner,
apiKey: apiKey
})
}).then(response => EJSON.parse(response.body.text()).access_token).then(accessToken => {
return http.post({
url: "https://stitch.mongodb.com/api/admin/v3.0/groups/<group-id>/apps/<app-id>/users",
headers: {
Authorization: ["Bearer " + accessToken]
},
body: JSON.stringify({
email: request.useremail,
password: request.userpass
})
});
});
};
这是使用 aws s3 服务的一个:
exports = function(payload) {
//base64EncodedImage, bucket, fileName, fileType
const body = EJSON.parse(payload.body.text());
// Convert the base64 encoded image string to a BSON Binary object
const binaryImageData = BSON.Binary.fromBase64(body.picture, 0);
// Instantiate an S3 service client
const s3Service = context.services.get('<aws-s3-service-title>').s3('<location>');
// Put the object to S3
return s3Service.PutObject({
'Bucket': '<aws-bucket-title>',
'Key': body.fileName,
'ContentType': body.fileType,
'Body': binaryImageData
})
.then(putObjectOutput => {
// console.log(putObjectOutput);
// putObjectOutput: {
// ETag: <string>, // The object's S3 entity tag
// }
return putObjectOutput;
})
.catch(console.error);
// return body;
};