Hi All,
I have planned to use mongoDB Stitch. But it provides browser SDK and server SDK. And tried both of them and i am getting some error. I believe this is because my app is isomorphic as i am using Next.js. Please guide me what can i do in such situation
> Ready on http://localhost:3000
Note that pages will be compiled when you first load them.
ReferenceError: self is not defined
at D:\simpliask\node_modules\whatwg-fetch\dist\fetch.umd.js:8:40
at support.searchParams (D:\simpliask\node_modules\whatwg-fetch\dist\fetch.umd.js:2:66)
at Object.<anonymous> (D:\simpliask\node_modules\whatwg-fetch\dist\fetch.umd.js:5:2)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (D:\simpliask\node_modules\mongodb-stitch-browser-core\dist\cjs\core\internal\net\BrowserFetchTransport.js:4:22)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
And when i use mongodb-stitch-server-sdk i get following exception.
Failed to compile. ./node_modules/fs-extra/lib/index.js Module not found: Can't resolve 'fs' in 'D:\simpliask\node_modules\fs-extra\lib'
I am using server sdk in getInitialProps as below.
Index.getInitialProps = async function () {
const {Stitch}=require('mongodb-stitch-server-sdk');
const client = Stitch.initializeDefaultAppClient('*********')
return {
clientd:client
}
}
Yuriy Boev
This is the example of the user registration and file post via mongodb stitch webhook: (So in this case you can just use fetch to execute proper 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: "stitch.mongodb.com/api/admin/v3.0/auth/providers/…",
body: JSON.stringify({
username: owner,
apiKey: apiKey
})
}).then(response => EJSON.parse(response.body.text()).access_token).then(accessToken => {
return http.post({
url: "stitch.mongodb.com/api/admin/v3.0/groups/<grou…",
headers: {
Authorization: ["Bearer " + accessToken]
},
body: JSON.stringify({
email: request.useremail,
password: request.userpass
})
});
});
};
This is the one that uses aws s3 service:
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;
};
What errors are you seeing?
Typically, you would use any server-side code in getInitialProps (you might check for request to make sure it only runs server-side). Browser SDK should probably be used on componentDidMount (or any regular React lifecycle method).
faisal ansari
Senior Software Engineer
Right now mongo-db Stitch framework does not provide a way to develop an app in isomorphic apps. Such as using Next.js. For more information you can have a look here