te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>node.js - How to share code in JavaScript Azure Functions? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to share code in JavaScript Azure Functions? - Stack Overflow

programmeradmin3浏览0评论

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 requireing 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 requireing 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?

Share Improve this question edited Jun 20, 2017 at 1:18 Gary Liu 13.9k1 gold badge20 silver badges33 bronze badges asked Jun 18, 2017 at 6:58 dougajmcdonalddougajmcdonald 20.1k12 gold badges60 silver badges95 bronze badges 4
  • 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
Add a ment  | 

1 Answer 1

Reset to default 18

I fixed this issue by doing the following steps:

  1. Add a line to the root hosts.json to watch a shared folder. "watchDirectories": [ "Shared" ]
  2. 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);
  1. 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 finds 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

发布评论

评论列表(0)

  1. 暂无评论