admin管理员组文章数量:1243218
How can I share code (e.g. Mongo schema definitions) between files in an Azure function app?
I need to do this, as my functions require access to a shared mongo schema and models, such as this basic example:
var blogPostSchema = new mongoose.Schema({
id: 'number',
title: 'string',
date: 'date',
content: 'string'
});
var BlogPost = mongoose.model('BlogPost', blogPostSchema);
I've tried to add a "watchDirectories": [ "Shared" ]
line to my host.json
and in that folder added an index.js
containing the above variable definition but this doesn't seem to be available to the other functions.
I simply get a Exception while executing function: Functions.GetBlogPosts. mscorlib: ReferenceError: BlogPost is not defined
.
I've also tried explitely require
ing the .js file, but this seems not to be found. It could be I just got the path wrong.
Does anyone have an example or tips on how to share .js
code between azure functions?
How can I share code (e.g. Mongo schema definitions) between files in an Azure function app?
I need to do this, as my functions require access to a shared mongo schema and models, such as this basic example:
var blogPostSchema = new mongoose.Schema({
id: 'number',
title: 'string',
date: 'date',
content: 'string'
});
var BlogPost = mongoose.model('BlogPost', blogPostSchema);
I've tried to add a "watchDirectories": [ "Shared" ]
line to my host.json
and in that folder added an index.js
containing the above variable definition but this doesn't seem to be available to the other functions.
I simply get a Exception while executing function: Functions.GetBlogPosts. mscorlib: ReferenceError: BlogPost is not defined
.
I've also tried explitely require
ing the .js file, but this seems not to be found. It could be I just got the path wrong.
Does anyone have an example or tips on how to share .js
code between azure functions?
-
The error would appear to be elsewhere since it's likely that
BlogPost
is actually a symbol pointing to the "Model" rather than the "Schema" definition. Also I doubt you can use mongoose in that way with Azure functions, since I'm "presuming" the supported functionality would be limited to just the "Schema" itself. See Mongoose in the browser. – Neil Lunn Commented Jun 18, 2017 at 7:22 -
Hi Neil, you're right the BlogPost var is a reference to the model. However, if I put the schema and model in a single Azure function things work how I'm expecting, but if I then add the schema and model to a second function I get an error from mongoose
Cannot overwrite 'BlogPost' model once piled
so I'm try to find a way I can define the schema and model once and re-use it – dougajmcdonald Commented Jun 18, 2017 at 7:28 - That is basically my thinking. Have not personally played around enough with Azure functions to be certain, so still on the tuit list. Unless they can actually spin up a full node instance and persist ( which I don't think is the point ) then I don't see such a thing working. Hard to tell without seeing the whole context. – Neil Lunn Commented Jun 18, 2017 at 7:31
- See below, managed to fix it with some minor tweaking – dougajmcdonald Commented Jun 18, 2017 at 7:41
1 Answer
Reset to default 18I fixed this issue by doing the following steps:
- Add a line to the root
hosts.json
towatch
a shared folder."watchDirectories": [ "Shared" ]
- In the shared folder, added a
blogPostModel.js
file containing the following schema/model definition and export
shared\blogPostModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var blogPostSchema = new Schema({
id: 'number',
title: 'string',
date: 'date',
content: 'string'
});
module.exports = mongoose.model('BlogPost', blogPostSchema);
- In my function
require
the shared file with the following path:var blogPostModel = require('../Shared/blogPostModel.js');
I can then make a connection and interact with the model doing find
s etc in each individual function.
This solution was posed from the following SO posts:
Azure Function in Node.js and shared files
Cannot overwrite model once piled Mongoose
本文标签: nodejsHow to share code in JavaScript Azure FunctionsStack Overflow
版权声明:本文标题:node.js - How to share code in JavaScript Azure Functions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740160527a2234342.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论