Tag: typescript

mongoose:为什么我没有得到错误的回应

我写这条路线find用户每次我input错误的用户我没有得到res.json()味精 app.post('/authenticate', function(req, res){ user.findOne({email : req.body.username}, function(err, user){ if(err){ res.json('user not found'); //not getting this }else{ console.log(user); //probably this ran, cuz getting null at cmd } }) })

找不到模块的声明文件

我正在尝试使用我创build的nodejs包,有人可以指出我做错了什么。 这里是包结构的样子: node_modules |__my_commons | |__dist | |__src | | |__helpers.d.ts | | |__helpers.js | | |__index.d.ts | | |__index.js | |__node_modules <=This got recursively installed | |__.npmignore | |__package.json src |_app/app.component.ts <= Here I am using it my_commons /的package.json { "_args": [ [ { "raw": "my_commons@bitbucket:ishan_dutta/my_commons", "scope": null, "escapedName": "my_commons", "name": "my_commons", "rawSpec": "bitbucket:ishan_dutta/my_commons", […]

将firebase函数转换为JavaScript的Typescript

我已经开始深入研究Firebase的云function,但是经过这么长时间,成为一名Android开发人员转而使用javascript已经非常艰难。 我已经使用firebase命令行工具(例如firebase login / init / deploy)configuration了一个新项目。 而且还需要npm和所有的依赖。 我正在使用Webstorm开发,并决定尝试按照YouTube上的video使用Typescript。 [ https://www.youtube.com/watch?v=GNR9El3XWYo&t=1106s] [1 ] 我已经设法得到我的Typescript编译,但是当转译为JavaScript时,它不能正确编译。 任何人都可以帮我解决这个问题吗? 下面的TS脚本 import * as functions from 'firebase-functions' import * as request from 'request-promise' export let addPlayerRequestNotification = functions.database .ref('notifications/{id}/notification') .onWrite(async event => { let notificaion = event.data.val(); let oauthToken = await functions.config().firebase.credential.getAccessToken(); await request ({ url: 'https://fcm.googleapis.com/fcm/send', method: 'POST', headers: […]

打字稿中的第三方模块的声明文件

我遇到了一些问题,把我刚刚为第三方软件包“ newrelic ”所做的声明文件整合起来。 总是当我运行tsc我得到了下一个错误信息: src/Server.ts(17,7): error TS2322: Type '{ express: typeof e; newrelic: typeof 'newrelic'; }' is not assignable to type 'BootServicesInterface'. 财产types“newrelic”是不相容的。 键入'typeof'newrelic''不能分配键入'newrelic'。 属性'setTransactionName'在'typeof'newrelic''types中缺less。 有没有人如何解决这个错误? 我已经工作了几个小时,我看不出我做错了什么。 源文件: ./src/Server.ts 'use strict' import * as debugDep from 'debug' const debug = debugDep('server') debug('Booting Server') debug('Loading .env file') import * as dotenv from 'dotenv' dotenv.config({silent: true}) debug('Loading […]

使用带有mongoose模式的TypeScript枚举

我有一个枚举模式: export interface IGameMapModel extends IGameMap, Document {} export const gameMapSchema: Schema = new Schema({ name: { type: String, index: { unique: true }, required: true }, type: { type: String, enum: CUtility.enumToArray(GameMode) } }); export const GameMap: Model<IGameMapModel> = model<IGameMapModel>('GameMap', gameMapSchema); GameMap是一个枚举。 第一个问题已经在这里:我需要将枚举转换为string数组,以便与模式一起使用。 其次,我想在模式创build期间直接使用枚举值。 new GameMap({ name: 'Test', type: GameMode.ASSAULT }); 返回ValidationError: type: '1' […]

在使用TypeScript的返回数组的mongoose查询中使用`lean`

我有两个Mongoose查询,并决定最好在它们上使用.lean() 。 对于返回单个文档的那个,它似乎工作正常: let something:Something; SomethingDocument.findOne({_id:theId}).lean().then( (res) => { something = res;}); 问题是当我尝试使用它返回多个结果的查询: let somethings:Something[]; SomethingDocument.find({color:'blue'}).lean().then( (res) => { somethings = res;}); 第二个电话给出错误: Type 'Object' is not assignable to type 'Something[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Property 'length' is missing in […]

如何在VSCODE上发布LSP语言服务器,就像我们做扩展一样

已经通过官方网站,整个发布扩展的过程是非常良好的文档: vscode发布扩展 我的疑问是关于在vscode中发布语言服务器(LSP),因为LSP的文件结构不同于扩展的文件结构,即扩展只有客户端,但LSP同时具有客户端和服务器目录,如同LSP代码和扩展代码 。 我是否需要在client和server目录中分别运行vsce publish ? 在这里已经通过各种答案在stackoverflow。 任何帮助将不胜感激。

Promise <T>的TypeScript参数,为什么不承诺<T1,T2>?

我想知道为什么Promise<T>不需要两个参数,如下所示: Promise<T1,T2> 。 例如: new Promise(function(resolve,reject){ … err ? reject(err) : resolve(val); }); =>🌷🌷🌷如何告诉消费者关于err 和 val两种types的承诺? 🌷🌷🌷 我希望T1是Errortypes, T2是val的types。 为什么不承诺采取两个types的参数? 因为它正式只有一个,我假设参数是传递给resolve()的值的types? 是否只有一个参数,因为我们期望一个Errortypes总是被传递给reject() ? 进一步的细节,我们可以传递一个string来拒绝: new Promise(function(resolve,reject){ let err = 'just a string, not an object'; let val = {foo:'bar'}; err ? reject(err) : resolve(val); }); 请注意,我们可以将错误强制为某种types ,如下所示: return function(){ return Promise.resolve('whatever') .catch(function(){ return Promise.reject('always a […]

TypeScript'导出分配不能用于具有其他导出元素的模块'

我的目标是为所有的redis操作提供一个redis.js文件,就像下面的代码片断一样 const Redis = require('./handler/redis.js'); Redis.set(); Redis.get() etc .. from my app.js require('./core/redis')(redisClient); 上面的代码运行没有任何错误但是我得到typescript错误为'一个导出分配不能用在一个模块与其他导出的元素' module.exports = function(redisClient) { redisClient.on('error',function(err) { console.log('Redis Error ' + err); }); redisClient.on('connected',function() { console.log('Redis Connected'); }); module.exports.set = function(key, value, next) { redisClient.set(key,value,next); }; module.exports.get = function(key, next) { redisClient.get(key,next); }; module.exports.del = function(key, next) { redisClient.del(key,next); }; }; 即使这个警告如何我的代码正在运行 […]

预编译为纯HTML的手把

有没有办法将Handlebars模板预编译为纯HTML? 把手预编译产生一个js文件。 我可以在节点上运行JS来生成一个HTML文件吗? 这就是我想要的: // Get a Handlebars instance var hb = require("handlebars"); // Load a template import fs = require('fs'); var template:string = fs.readFileSync('templates/template.handlebars','utf8'); // Compile said template var compiled = hb.precompile(template); // Write JS file fs.writeFileSync('compiled/template.js', compiled); var test = compiled.main(); // Write HTML file fs.writeFileSync('compiled/template.html', test); 这会失败,因为编译.main不是一个函数。 在那里有一个主要的function,我正在试图find。